0

我正在尝试在这里做一些非常简单的事情。我要做的就是更新 MySQL 中的信息这是表单下面的代码。

 <?php
 $host=""; // Host name 
 $username=""; // Mysql username 
 $password=""; // Mysql password 
 $db_name=""; // Database name 
 $tbl_name=""; // Table name 

 // Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

 // get value of id that sent from address bar
 $dj=$_GET['dj'];

 // Retrieve data from database 
 $sql="SELECT * FROM $tbl_name WHERE dj='$dj'";
 $result=mysql_query($sql);
 $rows=mysql_fetch_array($result);
 ?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update The information for the Now PlayingProgram.</strong>
</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Email2</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
<td>
<input name="email2" type="text" id="email2" value="<?php echo $rows['email2']; ?>" size="15">
</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Twitter</strong></td>
<td align="center"><strong>Twitter2</strong></td>
<td align="center"><strong>Avatar</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="twitter" type="text" id="twitter" value="<?php echo $rows['twitter']; ?>">
</td>
<td align="center">
<input name="twitter2" type="text" id="twitter2" value="<?php echo $rows['twitter2']; ?>" size="15">
</td>
<td>
<input name="avatar" type="text" id="avatar" value="<?php echo $rows['avatar']; ?>" size="15">
</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Facebook</strong></td>
<td align="center"><strong>Facebook2</strong></td>
<td align="center"><strong>Type</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="facebook" type="text" id="facebook" value="<?php echo $rows['facebook']; ?>">
</td>
<td align="center">
<input name="facebook2" type="text" id="facebook2" value="<?php echo $rows['facebook2']; ?>" size="15">
</td>
<td>
<input name="type" type="text" id="type" value="<?php echo $rows['type']; ?>" size="15">
</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Alias1</strong></td>
<td align="center"><strong>Alias2</strong></td>
<td align="center"><strong>Alias3</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="alias1" type="text" id="alias1" value="<?php echo $rows['alias1']; ?>">
</td>
<td align="center">
<input name="alias2" type="text" id="alias2" value="<?php echo $rows['alias2']; ?>" size="15">
</td>
<td>
<input name="alias3" type="text" id="alias3" value="<?php echo $rows['alias3']; ?>" size="15">
</td>
</tr>

<tr>
<td align="center">&nbsp;</td>
 <td colspan="3" align="center"><strong>Request Line</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="3" align="center">
 <input name="address" type="text" id="address" value="<?php echo $rows['address']; ?>" size="65">
 </td>
 </tr>
 <tr>
 <td>&nbsp;</td>
 <td>
 <input name="dj" type="hidden" id="dj" value="<?php echo $rows['dj']; ?>">
 </td>
 <td align="center">
 <input type="submit" name="Submit" value="Submit">
 </td>
 <td>&nbsp;</td>
 </tr>
 </table>
 </td>
 </form>
 </tr>
 </table>
  <?php
 // close connection 
 mysql_close();
 ?>

现在我想要做的是编辑当前显示在表单中的数据库中的信息。(表单显示来自 mysql 的正确信息)如果我想更改让我们说 email2 我会在字段中这样做,然后单击提交。那么我的问题是,当我这样做时,我得到的只是错误。

所以让我们看看我的 update_ac.php 代码如下:

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE currentdj SET  name='$name',
                        email='$email',
                        email2='$email2',                           
                        twitter='$twitter',
                        twitter2='$twitter2',
                        avatar='$avatar',                           
                        facebook='$facebook',
                        facebook2='$facebook2',
                        type='$type',
                        alias1='$alias1',
                        alias2='$alias2',
                        alias3='$alias3',
                        address='$address'                          
                        WHERE dj='$dj'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

我想知道我是否只是有语法甚至拼写问题!任何帮助,将不胜感激。

谢谢!

4

3 回答 3

2

UPDATE查询单引号未关闭email字段email='$email,

"UPDATE currentdj SET  name='$name',
                        email='$email',    //Add THE MISSING SINGLE QUOTE HERE
                        email2='$email2',
于 2012-10-08T03:41:38.440 回答
0

试试这段代码,在 where ,( address='$address', WHERE dj='$dj'";) 不应该出现 之前

$sql="UPDATE currentdj SET  name='$name',
                        email='$email',
                        email2='$email2',                           
                        twitter='$twitter',
                        twitter2='$twitter2',
                        avatar='$avatar',                           
                        facebook='$facebook',
                        facebook2='$facebook2',
                        type='$type',
                        alias1='$alias1',
                        alias2='$alias2',
                        alias3='$alias3',
                        address='$address'                        
                        WHERE dj='$dj'";
于 2012-10-08T03:46:47.443 回答
0

在您的 update_ac.php 文件中,您没有使用$_POST[]变量从表单中检索值。结果,没有值被插入到数据库中,因此出现错误。

将此代码放在具有 $sql="UPDATE..... 的更新查询语句之前

 $email=$_POST['email'];
 $email2=$_POST['email2'];                          
 $twitter=$_POST['twitter'];

等等...

于 2012-10-08T03:51:12.730 回答