Mysql 要使用更新查询编辑数据库吗
我想更新数据库中的记录,但出现错误,记录未更新。 我的问题:Mysql 要使用更新查询编辑数据库吗,mysql,sql,Mysql,Sql,我想更新数据库中的记录,但出现错误,记录未更新。 我的问题: 我首先处理数据库,运行selectquery,然后将数据插入数据库。我的select查询和insert查询正在运行,但当我更新我的记录并希望在其他页面中编辑时,我遇到了错误 我的代码 <?php $con=mysqli_connect("localhost","root","","new"); // Check connection if (mysqli_connect_errno()) { echo "
我首先处理数据库,运行selectquery,然后将数据插入数据库。我的select查询和insert查询正在运行,但当我更新我的记录并希望在其他页面中编辑时,我遇到了错误 我的代码
<?php
$con=mysqli_connect("localhost","root","","new");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM img");
echo "<table border='1'>
<tr>
<th>name</th>
<th>age</th>
<th>phonenum</th>
<th>email</th>
<th>date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo '<form action=""> ';
echo '<input type="hidden" name="rec_id" value="'.$row['id'].'" />';
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['phonenum'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><<a href=edit.php?id=$row[id]'>UPDATE</a></td>";
echo "</tr>";
echo '</form>';
}
echo "</table>";
mysqli_close($con);
$name = $age = $phonenum = $email = $date = $id="";
if ($_SERVER["REQUEST_METHOD"] == "GET")
{
$id = $_GET['id'];
if (empty($_POST["name"]))
{
$nameErr = "Name is required";
}
else
{
$name = ($_POST["name"]);
}
if (empty($_POST["age"]))
{
$emailErr = "Age is required";
}
else
{
$email = ($_POST["phonenum"]);
}
if (empty($_POST["email"]))
{
$password = "";
}
else
{
$password = ($_POST["password"]);
}
if (empty($_POST["date"]))
{
$phone = "";
}
else
{
$phone =($_POST["date"]);
}
// $name = $age = $phonenum = $email = $date = "";
}
$con=mysqli_connect("localhost","root","","new");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "UPDATE img SET name = '".$name."', age = '".$age."', phonenum = '".$phonenum."', email = '".$email."'
WHERE id = ".$id;
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
<div style="clear:both">
<form action="manage.php" method="post" style="margin-left:502px">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" autocomplete="off"/></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>Phone No</td>
<td><input type="text" name="phonenum" /></td>
</tr>
<td>E-mail</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Date</td>
<td><input type="text" name="date" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
我想我知道你在做什么:
你在这里发帖子:
<form action="manage.php" method="post" style="margin-left:502px">
您必须使用:
if ($_SERVER["REQUEST_METHOD"] == "POST")
以获取提交的值
你的代码中有很多错误的做法 一旦打印出更新查询并在mysql客户机中运行它,看看这是否有效,如果您发布了您得到的特定错误,它会有所帮助。
if ($_SERVER["REQUEST_METHOD"] == "POST")