如何使用PHP向mySQL添加行?

如何使用PHP向mySQL添加行?

问题描述:

I am trying to add an "admin" section of my website. Right now I am working on a section to add a new row to my MySQL database.

The first file is my admin.php:

<html>
...
<body>
<form action="add.php" method="post">
    <input type="text" name="order" />
    <input type="text" name="newstatus" />
    <input type="submit" value="Add" />

</form>

</body>
</html>

My goal here is to add 2 pieces of data (the table only has 2 columns right now) to the new row.

Then, I have my add.php file:

<?
//declare my connection variables - I'll move these to a secure method later
mysql_connect($servername, $username, $password) or die (mysql_error ());

// Select database
mysql_select_db($dbname) or die(mysql_error());

// The SQL statement is built

$sql="INSERT INTO $tblname(id, status)VALUES('$order', '$newstatus')";
$result=mysql_query($sql);

if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='admin.php'>Back to main page</a>";
}

else {
    echo mysqli_errno($this->db_link);
}
?>

<?php
// close connection
mysql_close();
?>

Anytime i input any data (non-duplicate of what is already in the table), it gives me an error. If I clear my table completely of all data, it will input once. I am getting a duplicate key error, but my key should be "orders", which is unique every time I input it.

Suggestions?

我正在尝试添加网站的“管理员”部分。 现在我正在开发一个部分,为我的MySQL数据库添加一个新行。 p>

第一个文件是我的admin.php: p>

 &lt; html&gt; 
 ... 
&lt; body&gt; 
&lt; form action =“add.php”method =“post”&gt; 
&lt; input type =“text”name =“order”/  &gt; 
&lt; input type =“text”name =“newstatus”/&gt; 
&lt; input type =“submit”value =“Add”/&gt; 
 
&lt; / form&gt; 
 
&lt;  ; / body&gt; 
&lt; / html&gt; 
  code>  pre> 
 
 

我的目标是将2个数据(表格现在只有2列)添加到新的 然后,我有我的add.php文件: p>

 &lt;?
 //声明我的连接变量 - 我 稍后将这些移动到安全方法
mysql_connect($ servername,$ username,$ password)或die(mysql_error()); 
 
 //选择database 
mysql_select_db($ dbname)或die(mysql_error())  ; 
 
 //构建SQL语句
 
 $ sql =“INSERT INTO $ tblname(id,status)VALUES('$ order','$ newstatus')”; 
 $ result = mysql_query(  $ sql); 
 
if($ result){
 echo“Successful”; 
 echo“&lt;  BR&gt;“; 
 
 
”&lt; a; href ='admin.php'&gt;返回主页&lt; / a&gt;“; 
} 
 
else {
 echo mysqli_errno($ this-&gt; db_link)  ; 
} 
?&gt; 
 
&lt;?php 
 //关闭连接
mysql_close(); 
?&gt; 
  code>  pre> 
 
 

随时 我输入任何数据(表中已有的数据不重复),它给我一个错误。 如果我完全清除所有数据的表格,它将输入一次。 我收到重复的密钥错误,但我的密钥应该是“订单”,每次输入时都是唯一的。 p>

建议? p> div>

If you are actually inserting a new row, you shouldn't fill the ID yourself but rather set it as AUTO_INCREMENT in your database. And then, have you form as such:

<form action="add.php" method="post">
    <input type="text" name="newstatus" />
    <input type="submit" value="Add" />
</form>

And your PHP code like so:

$newstatus = mysql_real_escape_string($_POST['newstatus']); // note the usage of $_POST variable
$sql="INSERT INTO `$tbl_name` (`status`) VALUES ('$newstatus')";
$result = mysql_query($sql) or die('Failed executing query: '.mysql_error());

AUTO_INCREMENT can be set up in phpMyAdmin with the following query:

ALTER TABLE `WorkOrders` MODIFY `id` INTEGER NOT NULL AUTO_INCREMENT;

Finally, don't use mysql_* functions, they are deprecated.

I am guessing the 'id' field of your table is a primary key. If that's the case, you cannot have two rows that have the same identifier.

By your variable name newstatus, it seems to me like you're trying to update the status of an order ; is it the case? If yes, you should use a UPDATE SQL query of the form:

UPDATE table SET status='somestatus' WHERE id=someid