doukong6031 2015-09-06 06:32
浏览 41
已采纳

从PHP更新mysql的最后一行

I try to update the last row in a table with user's input email the email column is optional

Html:

I used first form to return item details and update Mysql table - works fine, I put second input for email updates, if a user wants to receive updates

form action="actionemail.php" method="POST" onsubmit="return checkMail(this);">...</form>

The checkMail doesn't work

Javascript

function checkMail(){
 if ( theForm.checkbox.checked == false) 
 {
       alert('Did not check box');
       return false;
 } else {  
     return 'Thank you we will update you';
 }
}

I want to update the last row in table with the user email input to Email column, So I have two calls for server the form works fine

I used this code in php script (details are deleted)

<?
 php $servername = "localhost"; $username = ""; $password = "";
 $dbname ="";

 $conn = mysqli_connect($servername, $username, $password, $dbname);

 if (!$conn) {
   die("Connection failed: " . mysqli_connect_error()); }

  //$time = date('Y-m-d H:i:s');  //$IP = "$_SERVER[REMOTE_ADDR]";

 $email = $_POST["email"];

//$sql = "INSERT INTO enterlog (ItemCode, EnterDate, IPAddress, Email) VALUES ('$itemCode', '$time', '$IP', '$email')";


  $last = "SELECT MAX(LogID) FROM enterlog";

  $sql_update = "UPDATE enterlog SET Email='.$email.' WHERE logid=$last";

   if (mysqli_query($conn, $sql_update)) {
      echo "<h5>thank you"; } else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn); }

    mysqli_close($conn); 
?>

I get this error (new):

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT MAX(LogID) FROM enterlog' at line 1

I manage to add new line to table, I can't add the email optional input to the last line.

So what's wrong?

  • 写回答

1条回答 默认 最新

  • douchen2025 2015-09-06 06:49
    关注

    First change your php file starting from

    <?
     php
    

    to <?php

    and Change the following line

    $last = "SELECT MAX(LogID) FROM enterlog";
    $sql_update = "UPDATE enterlog SET Email='.$Email.' WHERE logid=$last";
    

    to:

    $last = "SELECT MAX(LogID) AS last_id FROM enterlog";
    $result = mysqli_query($conn, $last);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $last_id = $row['last_id'];
    
    $sql_update = "UPDATE enterlog SET Email='$Email' WHERE logid='$last_id'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器