dongyo7931 2019-05-02 07:10
浏览 63
已采纳

如何修复“插入”代码,该代码使用表单添加新记录但不向表中添加值[重复]

The code that i have written is a form that adds new record in to table called "listings". But in the table it shows me the new record with the id but the other values not showing or do not have import.

<?php

$con = mysqli_connect("", "", "", "");

    if($con === false)
      {
     die("ERROR: Could not connect. " . mysqli_connect_error());
      }
    $listings_title = $_POST['listings_title'];
    $listings_short_desc = $_POST['listings_short_desc'];
    $listings_description = $_POST['listings_description'];
    $listings_category = $_POST['listings_category'];
    $listings_tags = $_POST['listings_tags'];
    $listings_city = $_POST['listings_city'];
    $listings_address = $_POST['listings_address'];
    $listings_workingdays = $_POST['listings_workingdays'];

    $sql = "INSERT INTO listings2019 (listings_title, listings_short_desc, listings_description, listings_category, listings_tags, listings_city, listings_address, listings_workingdays) VALUES ('$listings_title', '$listings_short_desc',  '$listings_description', '$listings_category', '$listings_tags', '$listings_city', '$listings_address', '$listings_workingdays')";


if(mysqli_query($con, $sql)) {
    echo "New record created successfully.";
} else {
    echo "Error: Could not able to execute $sql. " . mysqli_error($con); 
}

mysqli_close($con);
    

?>
<html>
<body>
<form action="includes/db.php" method="post">

                    Title : <input name = "title" type = "text" >
                    <br/>

                    Description : <input name = "description" type = "text">
                    <br/>

                    Category : <input name = "category" type = "text">
                    <br/>

                    Tags :  <input name = "tags" type = "text">
                    <br/>

                    City : <input name = "city" type = "text">
                    <br/>

                    Address : <input name = "address" type = "text">
                    <br/>

                    Workingdays : <input name = "workingdays" type = "text">
                    <br/>
                   
                    Images: <input type="file" name="listing_image" />  
                     <br />  
                     <input type="submit" value="Insert"/> 

                        
                </form>  
</body>
</html>

stings_title, listings_short_desc, listings_description, listings_category, listings_tags, listings_city, listings_address, listings_workingdays) VALUES ('$listings_title', '$listings_short_desc', '$listings_description', '$listings_category', '$listings_tags', '$listings_city', '$listings_address', '$listings_workingdays')";

if(mysqli_query($con, $sql)) {
echo "New record created successfully.";
} else {
echo "Error: Could not able to execute $sql. " . mysqli_error($con); 
}

mysqli_close($con);
</div>
  • 写回答

3条回答 默认 最新

  • duanhu2414 2019-05-02 07:18
    关注

    Your indexes inside $_POST array don't match with the name attribute of the form inputs. For example

    Title :<input name = "title" type = "text" >
                    <br/>
    

    Its index should be like this

    $_POST['title']; // -> same as name = "title" inside form
    

    Same for the rest of other fields. For more info, you should visit this tutorial. I hope it will help you.

    Update

    One more advice. Always turn on php error reporting while development. It will help you in debugging and you can find errors by your self.

    Put this code at the top of php script

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    You can also turn on error reporting in php.ini file

    display_errors = on
    display_startup_errors = on
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料