2013-05-08 86 views
0

我有一个选择客户页面,当你点击一个表格行时,它应该设置当前正在构建的订单的客户ID。<tr onclick =“”>更新数据库ajax​​

这里是我已经,但它不拿起客户的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>" 

; 
+0

你有没有考虑过使用jQuery来简化你的代码? – Blazemonger 2013-05-08 17:11:05

+0

可爱的[SQL注入攻击](http://bobby-tables.com)在您的代码漏洞。享受你的服务器pwn3d。 – 2013-05-08 17:15:22

回答

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>"; 
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; 
0

而不是使用this.value

通过价值尝试

selectcust(" . $row[id] . ") 
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> 

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

OK,现在你的问题...

通过直接放置id在功能上,它应该通过id通过没有任何问题。