0

我有一个选择客户页面,当您单击表格行时,它应该设置当前正在构建的订单的客户 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

4 回答 4

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>";
于 2013-05-08T17:12:23.020 回答
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;
于 2013-05-08T17:21:45.140 回答
0

而不是使用传递值this.value

尝试

selectcust(" . $row[id] . ")
于 2013-05-08T17:59:04.557 回答
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可以毫无问题地通过。

于 2013-05-08T18:15:54.507 回答