更新数据库ajax

时间:2013-05-08 17:10:01

标签: php mysql ajax

我有一个选择客户页面,当您点击表格行时,它应该设置当前正在构建的订单的客户ID。

这里是我已经拥有但它没有拿起客户的ID,而是将客户设置为0而不是

function selectcust(str)

{
if (str=="")
  {
  document.getElementByid("description").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("description").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","php/insertcust.php?order=<?php echo $order; ?>&id="+str,true);
xmlhttp.send();
}

php是

$id = $_GET['id'];
$order = $_GET['order'];

include('config.php');

$sqlins = "UPDATE `sales` SET customernumber='$id' WHERE invoice = '".$order."'";

if (!mysql_query($sqlins,$con))
  {
  die('Error: ' . mysql_error());
  }
 echo $id;

,表格行为

echo"
<tr value='" . $row[id] . "' onclick='selectcust(this.value)'><td>" . $row['surname'] . "</td><td>" . $row['firstname'] . "</td><td>" . $row['Postcode'] . "</td><td>" . $row['Houseno'] . "</td><td>" . $row['org'] . "</td><td>" . $row[id] . "</td></tr>"

;

4 个答案:

答案 0 :(得分:0)

表行不应具有值或值属性。尝试使用唯一的ID属性:

echo "<tr id='" . $row[id] . "' onclick='selectcust(this.id)'><td> <input type='text' src='images/btn_delete.png' value='" . $row[id] . "' onfocus='selectcust(this.value)' height='30'/>" . $row['surname'] . "</td><td>" . $row['firstname'] . "</td><td>" . $row['Postcode'] . "</td><td>" . $row['Houseno'] . "</td><td>" . $row['org'] . "</td><td>" . $row[id] . "</td></tr>";

答案 1 :(得分:0)

现在,请用一些技巧让它变得可读。

$tr = <<<HTML
<tr id="{$row["id"]}" onclick="selectcust(this.id)">
    <td> 
        <input type="text" src="images/btn_delete.png" value="{$row[id]}" onfocus="selectcust(this.value)" height="30"/>
        {$row["surname"]}
    </td>
    <td>
        {$row["firstname"]}
    </td>
    <td>
        {$row["Postcode"]}
    </td>
    <td>
        {$row["Houseno"]}
    </td>
    <td>
        {$row["org"]}
    </td>
    <td>
        {$row["id"]}
    </td>
</tr>
HTML;

答案 2 :(得分:0)

而不是使用this.value

传递值

尝试

selectcust(" . $row[id] . ")

答案 3 :(得分:0)

这里有很多红旗,但我们无法照顾......

$mysqli = new mysqli(URL, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
$id = $_GET['id'];
$order = $_GET['order'];

include('config.php');

//The old mysql_query is now depreciated. Users now are to switch to mysqli or PDO
//The below is in mysqli and uses a prepared statement to protect against SQL injection
//attacks
$stmt = $mysqli->prepare("UPDATE `sales` SET customernumber=? WHERE invoice=?") or die("Error: " . $mysqli->error); //Set-up query, die and return error if it fails
$stmt->bind_param('ii', $id, $order); //Bind the paramaters to the query
$stmt->execute() or die("Error: " . $mysqli->error); //Execute the query or die and return the error if it fails

echo $id;

你的下一部分:

$stmt = $mysqli->prepare("SELECT id, surname, firstname, Postcode, Houseno, org FROM sales") or die("Error: " . $mysqli->error);
$stmt->execute() or die("Error: " . $mysqli->error);
$stmt->bind_result($custid, $surname, $firstname, $Postcode, $Houseno, $org); //Bind the results from the query to variables
$stmt->store_result(); //Store the result so you can do other queries without triggering errors
while($stmt->fetch()) {?>

    //Instead of echoing you can write the following OUTSIDE of the `<?php` tags and then
    //use the `<?= ?>` shortcut tags to output it where you need it.

    <tr value="<?=$custid?>" onclick="selectcust(<?=$custid?>)">
        <td>
            <input type='button' src='images/btn_delete.png' value="<?=$row[id]?>" onfocus="selectcust(<?=$custid?>)" height='30'/>
            <?=$surname?>
        </td>
        <td><?=$firstname?></td>
        <td><?=$Postcode?></td>
        <td><?=$Houseno?></td>
        <td><?=$org?></td>
        <td><?=$custid?></td>
    </tr>

你的意思是使用<input type='text'src=删除图片吗?你的意思是<input type='button'吗?

好的,现在问题......

id直接放在函数中,它应该通过id而不会出现任何问题。

相关问题