2013-05-08 59 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通過沒有任何問題。