简体   繁体   English

在php中插入mysql时出错

[英]Error while inserting into mysql in php

<?php
if (isset($_POST['submitted'])){
include('connect.php');
$name=$_POST['name'];
$addr=$_POST['addr'];
$phno=$_POST['phno'];
$tr=$_POST['tr'];
$trd=$_POST['trd'];
$sname=$_POST['sname'];
$sphno=$_POST['sphno'];
$sfield=$_POST['sfield'];
$med=$_POST['med'];
$apaid=$_POST['apaid'];
$apend=$_POST['apend'];

$sqlinsert="INSERT INTO patient (name , addr , phno , tname , tdate , sName , sphno , sSpeciality , med , amtpaid , amtrem) VALUES 
('$name','$addr','$phno','$tr','$trd','$sname','$sphno','$sfield','$med','$apaid','$apend')";

if(!mysql_query($dbcon , $sqlinsert))
{
        die('error inserting new record');
}
else
echo "new";
}
?>

<html>
<head>
    <title>Insert Data</title>
</head>
<body>
<h1>Insert Data</h1>
<form method="post" action="insert.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New Patient</legend>
<label>Name : <input type="text" name="name" /></label>
<label>Address : <input type="text" name="addr" /></label>
<label>Phone no : <input type="text" name="phno" /></label>
<label>Treatment : <input type="text" name="tr" /></label>
<label>Treatment date : <input type="text" name="trd" /></label>
<label>Specialist Recommended : <input type="text" name="sname" /></label>
<label>Specialist phno : <input type="text" name="sphno" /></label>
<label>Specialist field : <input type="text" name="sfield" /></label>
<label>Medicine : <input type="text" name="med" /></label>
<label>Amount Paid : <input type="text" name="apaid" /></label>
<label>Amount Pending : <input type="text" name="apend" /></label>
</fieldset>
<br />
<input type="submit"value="add new record" />
</form>
</body>
</html>

im getting this error while inserting even when the table has a string variable 即使表有一个字符串变量,插入时也会出现此错误

Warning: mysql_query() expects parameter 1 to be string, object given in C:\\xampp\\htdocs\\insert.php on line 18 error inserting new record 警告:mysql_query()期望参数1为字符串,在第18行的C:\\ xampp \\ htdocs \\ insert.php中给出的对象错误插入新记录

please help... 请帮忙...

Read the docs : 阅读文档

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

Side point: 侧点:

There is no more support for mysql_* functions, they are officially deprecated , no longer maintained and will be removed in the future. 不再支持 mysql_*函数,它们已被正式弃用不再维护 ,将来会被删除 You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. 您应该使用PDOMySQLi更新代码,以确保将来的项目功能。

check this manual. 查看本手册。 http://php.net/manual/en/function.mysql-query.php Parameter 1 is query and 2 is conn http://php.net/manual/en/function.mysql-query.php参数1是查询,2是conn

you are passing mysql connection object as argument to mysql_query() function . 您将mysql连接对象作为参数传递给mysql_query()函数。 mysql_query() expects only 1 string parameter . mysql_query()只需要1个字符串参数。 use mysqli_query() you have to change your code .. 使用mysqli_query()你必须改变你的代码..

<?php
if (isset($_POST['submitted'])){
include('connect.php');
$name=$_POST['name'];
$addr=$_POST['addr'];
$phno=$_POST['phno'];
$tr=$_POST['tr'];
$trd=$_POST['trd'];
$sname=$_POST['sname'];
$sphno=$_POST['sphno'];
$sfield=$_POST['sfield'];
$med=$_POST['med'];
$apaid=$_POST['apaid'];
$apend=$_POST['apend'];

$sqlinsert="INSERT INTO patient (name , addr , phno , tname , tdate , sName , sphno , sSpeciality , med , amtpaid , amtrem) VALUES 
('$name','$addr','$phno','$tr','$trd','$sname','$sphno','$sfield','$med','$apaid','$apend')";

if(!mysqli_query($dbcon , $sqlinsert))
{
        die('error inserting new record');
}
else
echo "new";
}

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM