doufangmu9087
2015-10-06 09:48
浏览 98
已采纳

警告:mysqli_error()期望参数1为mysqli,字符串为

I want to connect to a database (with xampp) called ranch and insert into this db some form data. The browser displays the error:

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\project1egister.php on line 38

ERROR: Could not able to execute INSERT INTO child_parent('childname','childsurname','age','gender','name','surname','address','tk','city','telephone','mobile','email','parea','pass') VALUES('nikos','ads','34','Αγόρι','sds','sds','dsd','34','dsds','34','434','mail@hotmail.com','sds','34').

I have the .php file that is below. How can i fix it?

<?php

ini_set('display_errors', 'On');
session_start();
unset ($msg);
    echo "kajsj<br>";
    $conn= new mysqli("localhost","root","","ranch");
    
    if (mysqli_connect_errno())
    { printf("Connect failed: %s
",mysqli_connect_error());//error message
    }
    else
    {
    printf("Connect achieved<br>"); 
    echo $_GET['childname'];
    $childname=$_GET['childname'];
    $childsurname=$_GET['childsurname'];
    $age=$_GET['age'];
    $gender=$_GET['gender'];
    $name=$_GET['name'];
    $surname=$_GET['surname'];
    $address=$_GET['address'];
    $tk=$_GET['tk'];
    $city=$_GET['city'];
    $telephone=$_GET['telephone'];
    $mobile=$_GET['mobile'];
    $email=$_GET['email'];
    $parea=$_GET['parea'];
    $pass=$_GET['pass'];
    // Insert data into mysql 
    $query1="INSERT INTO child_parent('childname','childsurname','age','gender','name','surname','address','tk','city','telephone','mobile','email','parea','pass')
                                VALUES('$childname','$childsurname','$age','$gender','$name','$surname','$address','$tk','$city','$telephone','$mobile','$email','$parea','$pass')";
    
    if(mysqli_query($conn, $query1))
    {echo "Records added successfully.";
    } 
    else{echo "ERROR: Could not able to execute $query1. " . mysqli_error($query1);
    }
    }
?>

</div>

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

我想连接到一个名为ranch的数据库(带xampp),并在此db中插入一些表单数据。 浏览器显示错误:

警告:mysqli_error()要求参数1为mysqli,字符串在C:\ xampp \ htdocs \ project1 egister中给出 。 第38行的php

错误:无法执行INSERT INTO child_parent('childname','childsurname','age','gender','name','surname) ','地址','tk','城市','电话','移动','电子邮件','parea','通过') VALUES('nikos','ads','34', 'Αγόρι', 'SDS', 'SDS', 'DSD', '34', 'DSDS', '34', '434', 'mail@hotmail.com', 'SDS', '34')。< / p>

我的.php文件位于下方。 我该如何解决?

 &lt;?php 
 
 
 
ini_set('display_errors','On'); 
 
session_start(); 
 
unset($ msg); 
 \  n echo“kajsj&lt; br&gt;”; 
 
 $ conn = new mysqli(“localhost”,“root”,“”,“ranch”); 
 
 
 
 if(mysqli_connect_errno())\  r 
 {printf(“连接失败:%s 
”,mysqli_co  nnect_error()); //错误消息
 
} 
 
其他
 
 {
 
 printf(“Connect achie&lt; br&gt;”);  
 
 echo $ _GET ['childname']; 
 
 $ childname = $ _ GET ['childname']; 
 
 $ childsurname = $ _ GET ['childsurname']; 
 
 $ age =  $ _GET [ '年龄']; 
 
 $的性别= $ _ GET [ '性别']; 
 
 $的名称= $ _ GET [ '名']; 
 
 $姓= $ _ GET ['姓 ']; 
 
 $的地址= $ _ GET [' 地址 ']; 
 
 $的TK = $ _ GET [' TK ']; 
 
 $的城市= $ _ GET [' 城市“]; 
  
 $的电话= $ _ GET [ '电话']; 
 
 $的移动= $ _ GET [ '移动']; 
 
 $的电子邮件= $ _ GET [ '电子邮件']; 
 
 $的parea =  $ _GET ['parea']; 
 
 $ pass = $ _ GET ['pass']; 
 
 //将数据插入mysql 
 
 $ query1 =“INSERT INTO child_parent('childname','  childsurname”, '年龄', '性别', '名', '姓', '地址', '传统知识', '城市', '电话', '移动', '电子邮件', 'parea', '及格'  )
 
 VALUES( '$ childname', '$ childsurname', '$年龄', '$性别', '$名', '$姓', '$地址', '$ TK',“$城市 ','$ telephone','$ mobile','$ email','$ parea','$ pass')“; 
 
 
 
 if(mysqli_query($ conn,$ query1))
 
  
 {echo“记录添加成功。”; 
 
} 
 
其他{echo“错误:无法执行$ query1。”。  mysqli_error(QUERY1 $); 
 \ N} 
 \ N} 
 
&GT;   
 
  
 
  
 \  n 
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyangqian5243 2015-10-06 09:53
    已采纳

    There are three main problems with your code:

    1. Wrong SQL. You have to learn basic SQL syntax prior trying to use SQL from PHP.
    2. Wrong mysqli syntax. You have to use prepared statements instead of adding variables right in the query.
    3. Wrong error reporting. You have to set mysqli in exception mode instead of checking every query result manually.

    To solve them one by one, read these three helpful answers:

    1. When to use single quotes, double quotes, and backticks in MySQL
    2. How can I prevent SQL injection in PHP?
    3. How to get mysqli error in different environments?
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题