MySQL UPDATE函数无效

时间:2016-03-21 08:37:59

标签: php mysql

我是PHP编码的新手,这是我必须做的一项任务。

我已经在文件中包含了DB Connect,但这些是我在此作业中使用的代码,可以编辑作业广告数据。

这是广告表文件。

$result = mysql_query("SELECT * FROM advertisement");
<TABLE border ='1'>
<table style="width:100%">
<tr>

<th>Advertisement ID</th>
<th>Position</th>
<th>Start Date</th>
<th>End Date</th>
<th></th>
<th></th>

</tr>

<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

echo "<TR>";

echo "<TD>".$row['Ad_ID']."</TD>";
echo "<TD>".$row['Position_Name']."</TD>";
echo "<TD>".$row['Start_date']."</TD>";
echo "<TD>".$row['End_date']."</TD>";
echo "<TD><a href='edit-advertisement.php?ad_id=".$row['Ad_ID']."'>Edit</a></TD>";
echo "<TD><a href='delete-advertisement.php?ad_id=".$row['Ad_ID']."'>Delete</a></TD>";

echo "</TR>";
}

?>

这是edit-advertisement.php文件。

$result = mysql_query("SELECT * FROM advertisement WHERE Ad_ID='".$_REQUEST['ad_id']."'");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
<form name = 'edit-advertise-form' method = 'POST' action = 'confirm-edit-adv.php'>
<br >
<input type='hidden' name='ad_id' value='<?=$row['Ad_ID']?>'>
Advertisement ID :  <?=$row['Ad_ID']?><br><br />
Position to be recruited : <input type = "Textbox" Name = "Pos_Name" value = '<?=$row['Position_Name']?>'><br><br>
Job Description: <br ><br />  <textarea name="Job_Des" rows="5" cols="40" value = '<?=$row['Ad_Job_Description']?>'> </textarea><br><br>
Job Qualification: <br ><br /> <textarea name="Job_Quali" rows="5" cols="40" value = '<?=$row['Ad_Job_Qualification']?>'> </textarea><br><br>
Skill required: <br ><br /> <textarea name="Skill_Req" rows="5" cols="40"value = '<?=$row['Ad_Skill_Required']?>'> </textarea><br><br>
Salary offer: <input type = "Textbox" Name = "Salary" value = '<?=$row['Position_Salary_Detail']?>'><br><br>

Start date :
<SELECT name='s_day'>
<?php
$i = 1 ;
while($i<=31) {
?>
<OPTION value = '<?php echo $i;?>' >    <?php echo $i;?>    </OPTION>
<?php $i++; }
?>
</SELECT>

<SELECT name='s_month' >
<?php
$month = array( 1=> JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER);
$i = 1;
foreach ($month as $m){
?>  
<OPTION value = '<?php echo $i;?>' > <?php echo $m;?> </OPTION>
<?php
$i++;}  
?>
</SELECT>

<SELECT name = 's_year'>
<?php 
$curYear = getdate();
for($year = 2016 ; $year <= $curYear['year']; $year++){
?>
<OPTION value = '<?php echo $year;?>'> <?php echo $year;?> </OPTION> 
<?php
}   
?>
</SELECT>

End date -> Same as Start date

<input type = 'Submit' name = 'edit-adv' value = 'Update'><br><br>
<button onclick="goBack()">Back</button>

</form>

最后是更新功能页面

        $ad_id = $_POST["ad_id"];
        $pos_name = $_POST["Pos_Name"];
        $job_des = $_POST["Job_Des"];
        $job_qua = $_POST["Job_Quali"];
        $skill_req = $_POST["Skill_Req"];
        $salary = $_POST["Salary"];
        $s_date = $_POST["s_year"].'/'.$_POST["s_month"].'/'.$_POST["s_day"];
        $e_date = $_POST["e_year"].'/'.$_POST["e_month"].'/'.$_POST["e_day"];


        $sql = ("UPDATE advertisement SET Position_Name = '".$pos_name."',
        Ad_Job_Description = '".$job_des."', Ad_Job_Qualification = 
        '".$job_qua."', Ad_Skill_Required = '".$skill_req."', 
        Position_Salary_Detail = '".$salary."', Start_date = '".$s_date."', 
        End_date = '".$e_date."' WHERE Ad_ID = '".$ad_id."'");

我不知道$ sql行中有什么问题或什么。

我试图回显$ sql,没有任何东西,所以这意味着$ sql中没有值吗?

如何解决这个问题? 先感谢您 !!! PS。抱歉代码很长

1 个答案:

答案 0 :(得分:0)

在新版本中 连接

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD);
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysqli_select_db($con, DB_NAME) or die('Could not select database.' . mysql_error());

然后像这样更新你的数据库表

$qr = mysqli_query($con, "UPDATE advertisement SET Position_Name= '$pos_name',Ad_Job_Description= '$job_des' WHERE Ad_ID= '$ad_id' ");