[英]Why isn't my insert data code working?
I am trying to insert data into a database mysql phpAdmin.我正在尝试将数据插入数据库 mysql phpAdmin。
My webhost is 000webhost.我的虚拟主机是 000webhost。
My connection to mysql database code:我连接mysql数据库的代码:
<?PHP
$mysql_host = "mysql2.000webhost.com";
$mysql_database = "*********";
$mysql_user = "********";
$mysql_password = "**********";
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
if (!$dbcon) {
die('error connecting to database');
}
echo ('You have connected successfully');
?>
My insert data code:我的插入数据代码:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysql_query($dbcon, $sqlinsert)) {
die('error inserting new record');
} // end of nested if statement
$newrecord = "1 record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert-data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>
Instead of letting me put it into the database it brings me to this page http://error404.000webhost.com/ ?而不是让我把它放入数据库它把我带到这个页面http://error404.000webhost.com/ ?
try change尝试改变
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
to到
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
and also insert query and connection to并插入查询和连接到
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
First change below code:首先更改以下代码:
$dbcon = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
To :到 :
$dbcon = mysql_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
Then change below code:然后更改以下代码:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('fname', 'lname')";
To:到:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
can u please change this line?你能改变这条线吗?
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text name="fname" /></label>
<label>Last Name: <input type="text name="lname" /></label>
</fieldset>
to到
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
you are setting wrong attributes so its not getting the values..你设置了错误的属性,所以它没有得到值..
after that, u have to update your sql query:之后,您必须更新您的 sql 查询:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
It DID have to be mysqli它确实必须是 mysqli
my problem was the - had to be changed to an _ on <form method="post" action="insert-data.php">
我的问题是 - 必须在<form method="post" action="insert-data.php">
上更改为 _
working code:工作代码:
<?PHP
if (isset($_POST['submitted'])) {
include('connect_mysql.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
if (!mysqli_query($dbcon, $sqlinsert)) {
die('error inserting new record');
}
$newrecord = "1 new record added to the database";
}
?>
<html>
<head>
<title>Insert Data into DB</title>
</head>
<body>
<h1>Insert Data into DB</h1>
<form method="post" action="insert_data.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
<legend>New People</legend>
<label>First Name: <input type="text" name="fname" /></label>
<label>Last Name: <input type="text" name="lname" /></label>
</fieldset>
<br />
<input type="submit" value="add new person" />
</form>
<?PHP
echo $newrecord
?>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.