我一直在处理这个问题,但我在这里看不到解决方案。 我正在创建一个简单的列表应用程序,每当用户创建一个新项目时,它应该保存到数据库中。 问题是每个项目都有一个唯一的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。
非常感谢你!!
I've been dealing with that problem for a while, but I can't see the solution here. I am making a simple list application, and anytime the user creates a new item it should be saved to the database. The issue is that every item has a unique ID (auto-incremented), and that ID has to be on the html code when the item is appended to the list (other functions need to use it, so when it's uploaded to the database, then the ID should be selected and sended again to the original html file, but I'm not sure how to do that (in fact, I've got no idea).
Here's my php/html code:
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>'; }The 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); }And the 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());There is also much more code, but I posted what I think is the most important part. It works perfectly now, I just need to send the ID.
Thank you very much!!
最满意答案
您应该避免使用mysql_ *并使用PDO。 自PHP5.5起,Mysql_函数被删除
无论如何,您可以检索生成的最后一个id: mysql_insert_id()并将其返回到您的ajax处理程序,然后您可以使用var id = xmlhttp.responseText;获取它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 IDJS:
ajax.onreadystatechange=function() { if (ajax.readyState==4) { .... var id = ajax.responseText; // get it } }You should avoid using mysql_* and use PDO instead. Mysql_ functions are deprated since PHP5.5
Anyway you could retrieve the last id generated with : mysql_insert_id() and return it to your ajax handler, then you can get it with 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 IDJS :
ajax.onreadystatechange=function() { if (ajax.readyState==4) { .... var id = ajax.responseText; // get it } }更多推荐
发布评论