I am trying to insert data into a database mysql phpAdmin.
My webhost is 000webhost.
My connection to mysql database code:
<?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/ ?
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:
$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";
It DID have to be mysqli
my problem was the - had to be changed to an _ on <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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.