doumao6212
2014-11-21 12:44
浏览 308
已采纳

在数据库表中使用mysqli插入数据不起作用?连接工作正常

  • It should say new record and then I should be able to see new data in items table

  • connection to database file contains:

        $server="localhost";
        $login="root";
        $pass="";
        $database="floweritem";
    
        $con=mysqli_connect($server,$login,$pass,$database);
        if(mysqli_connect_errno()){
        echo "Failed connect to MYSQL".mysqli_connect_error();
        }
    
  • form action

  • PHP

        <?php
        if(isset($_POST['submit'])){
        $Name=mysqli_real_escape_string($con,$_POST['Name']);
        $Desc=mysqli_real_escape_string($con,$_POST['Desc']);
        $Size=mysqli_real_escape_string($con,$_POST['Size']);
        $Price=mysqli_real_escape_string($con,$_POST['Price']);
    
    
        //no problwm with connection to table or database
        $sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`) 
        VALUES($Name,$Desc,$Size,$Price)";
        echo $Desc;
    
    
        if(mysqli_query($con,$sql)){ 
        echo "new record";
        }
        //always get wrong
        //can't find problem
        else{echo"Wrong";}
        mysqli_close($con);
        }
        ?>
    
  • shows $Desc But Not successful to insert data in items table

图片转代码服务由CSDN问答提供 功能建议

  • 它应该说新记录然后我应该能够看到新数据 项目表

  • 与数据库文件的连接包含:

      $ server =“localhost”; 
     $ login =  “root”; 
     $ pass =“”; 
     $ database =“floweritem”; 
     
     $ con = mysqli_connect($ server,$ login,$ pass,$ database); 
     if(mysqli_connect_errno()  ){
     echo“连接到MYSQL失败”.mysqli_connect_error(); 
    } 
        
     
  • 表单操作

  • PHP

     &lt;?php 
     if(isset($ _ POST ['submit'])){
     $ Name = mysqli_real_escape_string  ($ con,$ _ POST ['Name']); 
     $ Desc = mysqli_real_escape_string($ con,$ _ POST ['Desc']); 
     $ Size = mysqli_real_escape_string($ con,$ _ POST ['Size'])  ; 
     $ Price = mysqli_real_escape_string($ con,$ _ POST ['Price']); 
     
     
     //没有连接到表或数据库的问题
     $ sql =“INSERT INTO`itements`(`Name  `,`Description`,`Size`,`Price`)
     VALUES($ Name,$ Desc,$ Size,$ Price)“; 
      echo $ Desc; 
     
     
     if(mysqli_query($ con,$ sql)){
     echo“new record”; 
    } 
     //总是出错
     //找不到问题\  n else {echo“Wrong”;} 
     mysqli_close($ con); 
    } 
    ?&gt; 
        
     
  • 显示$ Desc 但未成功在项目表中插入数据

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzha6055 2014-11-21 12:46
    已采纳

    You need to add single quotes against values.

    Corrected Code:

     //no problwm with connection to table or database
        $sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`) 
        VALUES('$Name','$Desc','$Size','$Price')";
        echo $Desc;
    

    Otherwise, MySQL will consider user entered values as MySQL keywords, hence creating Syntax errors.

    点赞 打赏 评论
  • dongyoufo5672 2014-11-21 12:53

    New php code should be like this

    <?php
        if(isset($_POST['submit'])){
        $Name=mysqli_real_escape_string($con,$_POST['Name']);
        $Desc=mysqli_real_escape_string($con,$_POST['Desc']);
        $Size=mysqli_real_escape_string($con,$_POST['Size']);
        $Price=mysqli_real_escape_string($con,$_POST['Price']);
    
    
        //no problwm with connection to table or database
        $sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`) 
        VALUES('$Name','$Desc','$Size','$Price')";
        echo $Desc;
    
    
        if(mysqli_query($con,$sql)){ 
        echo "new record";
        }
        //always get wrong
        //can't find problem
        else{echo"Wrong";}
        mysqli_close($con);
        }
        ?>
    
    点赞 打赏 评论
  • duangu1878 2014-11-21 12:57

    Try this its working :

    PHP :

    <?php
        if(isset($_POST['submit'])){
        $Name=mysqli_real_escape_string($con,$_POST['Name']);
        $Desc=mysqli_real_escape_string($con,$_POST['Desc']);
        $Size=mysqli_real_escape_string($con,$_POST['Size']);
        $Price=mysqli_real_escape_string($con,$_POST['Price']);
    
    
        //no problwm with connection to table or database
        $sql="INSERT INTO `items`(`Name`,`Description`,`Size`,`Price`) 
        VALUES('$Name','$Desc','$Size','$Price')";
        echo $Desc;
    
    
        if(mysqli_query($con,$sql)){ 
        echo "new record";
        }
        //always get wrong
        //can't find problem
        else{echo"Wrong";}
        mysqli_close($con);
        }
        ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题