Jump to content

can't input data with my form in php


phpnewbie26

Recommended Posts

Hi, I am having an Issue inputting data into my database with this code.

I tested the connection, which works fine, but when I try to submit the form I get the "die("lid query: " . mysql_error());".

Any Ideas?

 

 

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form action="thispage.php" method="POST">
<input type="text" name="name" placeholder="Check-in Name">
<input type="submit" value='submit'></form>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$host ="localhost";
$username = "username";
$password = "password";
$dbname = "database";
$con = mysql_connect($host, $username, $password, $dbname);
//Check Connection
if (!$con) {
die("Could not connect: " . mysql_error());
}
//Select your database
mysql_select_db("database",$con);
//Check to make sure the database is there
if (!$mysql_select_db) {
die ('Can't use the db : ' . mysql_error());
}
//Run query
$insert = mysql_query("insert into Users(name) values ('$name')");
}
//Check Query
if (!$insert) {
die("lid query: " . mysql_error());
}
echo "Data inserted";
mysql_close($con);
?>
</body>
</html>
Link to comment
Share on other sites

Well, what does the error say? You're printing an error message for a reason, the error message helps to figure out why something isn't working.

 

Your code is vulnerable to SQL injection, making your site really easy to hack. You should do two things:

1. Don't use the mysql library because it's vulnerable to security threats, change to mysqli or PDO.

2. Learn prepared statements to ensure without a doubt that you won't have problems with SQL injection.

Link to comment
Share on other sites

The script gets to the //Check Query part and fails. It echos out the "lid query" and doesn't put anything into the database.

I tried to just remove that portion, but it still wouldn't input any data into my table. I am very new at PHP and am basically

getting code off the internet and throwing it into some pages and seeing if it works. This is a very simple script, I just dont

see why it isn't working?

Link to comment
Share on other sites

It sounds like $_POST['submit'] isn't set, so the query isn't being executed. You should put your code in a [CODE][/code] tags so we can see it properly. Here's what your code looks like:

<?phpif (isset($_POST['submit'])) {    $name = $_POST['name'];    $host ="localhost";    $username = "username";    $password = "password";    $dbname = "database";    $con = mysql_connect($host, $username, $password, $dbname);    //Check Connection    if (!$con) {        die("Could not connect: " . mysql_error());    }    //Select your database    mysql_select_db("database",$con);    //Check to make sure the database is there    if (!$mysql_select_db) {        die ('Can't use the db : ' . mysql_error());    }    //Run query    $insert = mysql_query("insert into Users(name) values ('$name')");}// This section of code runs regardless of whether $_POST['submit'] exists or not//Check Queryif (!$insert) {    die("lid query: " . mysql_error());}echo "Data inserted";mysql_close($con);?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...