我处理那个问题已经有一段时间了,但我在这里找不到解决办法。我正在制作一个简单的列表应用程序,每当用户创建一个新项目时,它都应该保存到数据库中。问题是每个项目都有一个唯一的ID (自动递增),当项目被附加到列表时,ID必须在html代码中(其他函数需要使用它,所以当它被上传到数据库时,ID应该被选中并再次发送到原始html文件,但是我不知道该如何做(事实上,我不知道)。
下面是我的php/html代码:
while($row = mysql_fetch_array($selectitems) ){
echo'<html>
<li class="iÂtem" data-id="';
echo $row['IDitem'];
echo '">
<div class="draggertab"><img src="imatges/botofletxa.jpg" width="30" height="30"></div>
<div class="deletetab"><img src="imatges/botocreu.jpg" width="30" height="30"></div>
<span class="item">';
echo $row['Text'];
echo'</span>
</li>
</html>';
}javascript:
ajax.onreadystatechange=function() {
//la función responseText tiene todos los datos pedidos al servidor
if (ajax.readyState==4) {
//mostrar resultados en esta capa
AppendItem();
//llamar a funcion para limpiar los inputs
LimpiarCampos();
}
}
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//enviando los valores a registro.php para que inserte los datos
ajax.send("nombre="+nom+"&IDllista="+llista);
}和PHP:
$con = mysql_connect($bd_host, $bd_usuario, $bd_password);
mysql_select_db($bd_base, $con);
//variables POST
$nom=$_POST['nombre'];
$IDllista = $_POST['IDllista'];
//registra los datos del empleados
$sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')";
mysql_query($sql,$con) or die('Error. '.mysql_error());还有更多的代码,但我发布了我认为最重要的部分。它现在工作得很好,我只需要发送ID。
非常感谢!!
发布于 2013-12-20 20:49:37
您应该避免使用mysql_*,而应该使用PDO。自PHP5.5以来,Mysql_函数被降级
无论如何,您可以检索使用:mysql_insert_id()生成的最后一个id并将其返回到ajax处理程序,然后可以使用var id = xmlhttp.responseText;获取它。
PHP:
$sql="INSERT INTO items (Text, IDllista) VALUES ('$nom', '$IDllista')";
mysql_query($sql,$con) or die('Error. '.mysql_error());
echo mysql_insert_id(); // return the ID联署材料:
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
....
var id = ajax.responseText; // get it
}
}https://stackoverflow.com/questions/20711536
复制相似问题