Following is my error free code, it has no syntactical error but when I run it it executes the else part, which means my databse has not been updated.What is the problem?
<html>
<body>
<?php
$connect=mysql_connect("localhost","root","ila");
$query="create database if it not exists demo";
$db=mysql_select_db("demo",$connect);
$query= "create table jlt(id int not null , name varchar(49))";
$result=mysql_query($query);
$query="insert into jlt values(1,'$_POST[inputfield]')";
if(mysql_query($query,$connect)){
echo "data successfully added";
}
else {
echo "not success";
}
?>
</body>
</html>

Dani AI

Generated

Quick expert note for and everyone following this thread.

The script is going into the else branch because the INSERT call is returning false — the database server is rejecting the query. Common root causes here are: a SQL syntax error (your CREATE DATABASE line uses the words “if it not exists”, which is not valid SQL), the script never successfully selected the demo database, the CREATE TABLE failed (so the table isn’t present), or the INSERT itself is failing (for example because you keep inserting the literal id 1 and hit a duplicate-key error, or because the posted value contains an unescaped quote and breaks the SQL). These are all diagnosable with the database error message.

Practical debugging steps (build on ’s idea): after each mysql call check its return value and read the server error string. Call the error-reporting function immediately after a failed query to see the exact reason (syntax error, duplicate entry, unknown database, etc.). Echo the exact SQL you are sending and paste that into phpMyAdmin or the mysql client — that will show the same error text and point to the fix fast.

Fixes and best-practice follow-ups (expand on and ): explicitly name columns in INSERT and don’t hard-code an id — make the id an AUTOINCREMENT primary key so repeated runs won’t fail. Never inject raw POST data into SQL; use parameterized queries (mysqli or PDO prepared statements) or, at minimum, escape values. Note that the old mysql* extension is deprecated and removed from recent PHP versions — migrate to mysqli or PDO for security and future compatibility.

Quick checklist: correct the CREATE DATABASE syntax, verify mysql_select_db succeeded, check each mysql_query return and mysql_error() message, give id AUTO_INCREMENT, specify columns in INSERT, and migrate to prepared statements. Following these steps will reveal the actual error and make the insert succeed.

Recommended Answers

All 4 Replies

You haven't specified the field names in your insert query:

INSERT INTO `jlt`(`id`, `name`) VALUES (1, 'example');

And by not escaping your input variables, you're leaving the script open to SQL injection attacks.

As a general approach, if you are having a problem with a query, the fastest way to sort it out is to echo the query, paste it into PHPMyAdmin and work on it there until you have it figured out. Then you can change your code accordingly.

commented: yes +5

Try following code

<html>
<body>
<?php
$connect=mysql_connect("localhost","root","ila");
$query="create database if it not exists demo";
$db=mysql_select_db("demo",$connect);
$query= "create table jlt(id int not null , name varchar(49))";
$result=mysql_query($query);
$in=$_POST['inputfield'];
$query="insert into jlt values(1,'$in')";
if(mysql_query($query,$connect)){
echo "data successfully added";
}
else {
echo "not success";
}
?>
</body>
</html>

well yes, i need to import data into database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.