Okay, I have searched for an error in this code for days and haven't succeeded. This is supposed to insert a new user (new row) into a table. For some reason, it does absolutely nothing to the mysql table "users." I can connect and retrieve data from the table perfectly fine. Please help!
include('config.php');
$db = mysql_connect('localhost', $user, $pass);
if(!$db)
{
echo 'Cannot find the database';
exit;
}
$dbname = $user;
mysql_select_db($dbname) or die('Cannot select the database');
(other code that I have not posted because I have tested it without this code and it produces the same result)
$query = "INSERT INTO users VALUES (
NULL,
'$Passw' ,
'$Fname' ,
'$Lname' ,
'$Email' ,
'$Add1' ,
'$Add2' ,
'$City' ,
'$State' ,
'$Zip ,
'$Country' ,
'$Phone' ,
'$Bio')";
$result = mysql_query($query);
1 : Retrieve the possible MySQL error using:
if ($result === false)
echo mysql_error();
2 : It's a good practice to specify the table fields when you use INSERT
INSERT INTO table (field1, field2, field3) VALUES ('value1', 'value2', 'value3');
Because if you don't, and your DB structure changes, you will have to update the VALUES(...) part.
3 : use mysql_real_escape_string() when inserting strings in queries for MySQL, to avoid SQL injection problems.
Related
I have a problem with SQL syntax. I have 2 tables and the third table is generated in SQL Workbench (n:m relationship).
My 3rd table has 2 columns product_id and categories_id.
I use this SQL in php my admin to add a new row:
INSERT INTO `products_has_categories` (`products_id`, `categories_id`)
VALUES ('17', '1');
if phpmyadmin, the sql add a new row, with product_id = 17 and categories_id =1.
My problem:
i have a simple php file called test.php looking like:
$connection = mysqli_connect("andrei.local","andrei94ro","masina", "intership");
if(!$connection)
{
echo 'error';
}
$query = "INSERT INTO `products_has_categories` (`products_id`, `categories_id`) VALUES ('17', '1');";
or
$query = "INSERT INTO `products_has_categories` (`products_id`, `categories_id`) VALUES ('17', '1')";
When i run test.php file, the code not working, and no adding new row in SQL table.
Can u help me?
As far I'm concerned, you're not even calling query().
Try this:
$connection = new mysqli("andrei.local","andrei94ro","masina", "intership");
if(!$connection)
{
echo 'error';
} else {
$query = $connection->query("INSERT INTO `products_has_categories` (`products_id`, `categories_id`) VALUES ('17', '1')");
}
When I submit the form and use this script to insert the data in the db i get the error mentioned above...any ideas?
//Include connect file to make a connection to test_cars database
include("prototypeconnect.php");
$proCode = $_POST["code"];
$proDescr = $_POST["description"];
$proManu = $_POST["manufacturer"];
$proCPU = $_POST["cost_per_unit"];
$proWPU = $_POST["weight_per_unit"];
$proBarCode = $_POST["bar_code"];
$proIngredients = $_POST["ingredients_list"];
$proAllergens = $_POST["allergens_contains"];
$proMayAllergens = $_POST["allergens_may_contain"];
//Insert users data in database
$sql = "INSERT INTO prodb.simplex_list
code, description, manufacturer,
cost_per_unit, weight_per_unit, bar_code,
ingredients_list, allergens_contains,
allergens_may_contain)
VALUES
( '$proCode', '$proDescr' , '$proManu',
'$proCPU' , '$proWPU' , '$proBarCode',
'$proIngredients' , '$proAllergens',
'$proMayAllergens')";
//Run the insert query
if (!mysql_query($sql)) {
echo mysql_error();
}
?>
UPDATE: I removed id inserts as they are auto-increment and i learned from your answers that a null does not need to be coded and mysql looks after AI. Thanks guys!
Query need to be like:-
$sql = "INSERT INTO prodb.simplex_list
(code, description, manufacturer,
cost_per_unit, weight_per_unit,
bar_code, ingredients_list, allergens_contains,
allergens_may_contain)
VALUES ('$proCode', '$proDescr', '$proManu',
'$proCPU','$proWPU', '$proBarCode',
'$proIngredients', '$proAllergens',
'$proMayAllergens')";
Note:- please stop using mysql_*. Use mysqli_* or PDO. Also this will work only when id field must be auto incremented.
I'm trying to insert data in mysql using php I have FK with parishionerID
my query is correct but I always get the Error querying database. It doesn't show me
the specific query that I'm having an error with. BapID is PK. Hope you can help me resolve this. Thanks.
$con = mysqli_connect('localhost', 'root', '', 'RMS') or die('Error connecting to MySQL server');
$sql="INSERT INTO baptismal (ParishionerID, BapID, Datebaptism, Ministerbaptism, Sponsor, Sponsor2, Volume, Page)
VALUES('$id', NULL, '$Datebap', '$Ministerbap', '$Sponsor', '$Sponsor2, '$Volume', '$Page')";
$result = mysqli_query($con, $sql) or die('Error querying database.');
mysqli_close($con);
If I input datas in mysql manually this is the query which is pretty have the same with my query.
INSERT INTO `rms`.`baptismal` (`ParishionerID`, `BapID`, `Datebaptism`, `Ministerbaptism`, `Sponsor`, `Sponsor2`, `Volume`, `Page`) VALUES ('80', NULL, '2014-07-16', '131', 'sdasd', '123123', '123123', '123');
There seems to be only Syntax error. Youre missing '
VALUES('$id', NULL, '$Datebap', '$Ministerbap', '$Sponsor', '$Sponsor2, '$Volume', '$Page')";
----------------------------------------------------------------------^
Your $Sponsor2 is not properly quoted.
FYI: Let the script print out the error SQL too.
Convert your line as:
$result = mysqli_query($con, $sql) or die('Error querying database: '.$sql);
Then, you will see the exact query that ran.
Do not print SQLs on the server mode.
I have database contain table named demnads
table contain 11 cell first one is id auto increasement
I want to add data's with this sql command in php :
<?php
$hostname_mystore = "localhost";
$database_mystore = "mystore";
$username_mystore = "root";
$password_mystore = "";
$mystore = mysql_pconnect($hostname_mystore, $username_mystore, $password_mystore) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
if (mysqli_connect_errno())
{ echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= "INSERT into demands ( itemname , case , cname , fatora , client , cdate , ctime , prices , cmobile , numbers) values('$item[$i]','$cases[$i]','$customer[$i]','$fatora[$i]','$client[$i]','$dates[$i]','$times[$i]','$price[$i]','$mobile[$i]','$numbers[$i]' )";
?>
but data's didn't be inserted
You have switched from the mysql library (which is deprecated) to mysqli:
mysqli_connect_errno()
You should use one or the other.
Also, embedded array values need to be enclosed in curly brackets{$item[$i]}.
This assumes, of course, that these arrays and $i are defined elsewhere.
And you haven't shown the statement that actually inserts the data.
You forgot to call mysql_query( $sql, $conn )
Enclose your column names with backticks. case is a keyword within MySQL and as such can not be used as a column name without the backticks!
$sql= "INSERT into demands ( `itemname` , `case` , `cname` , `fatora` , `client` , `cdate` , `ctime` , `prices` , `cmobile` , `numbers`) values('$item[$i]','$cases[$i]','$customer[$i]','$fatora[$i]','$client[$i]','$dates[$i]','$times[$i]','$price[$i]','$mobile[$i]','$numbers[$i]' )";
You said your table name is demnads . But in Coding you used demands?
Check again.
I need to retrieve the auto increment field from my database table. I tried the following but $id is always just empty.
The insert works too.
My table is as follows:
idint(9) NOT NULL auto_increment,
and id is set as primary
What am I doing wrong?
$conn = mysql_connect($host,$username,$password);
mysql_select_db($database, $conn) or die( "Unable to select database");
include "update_activity.php";
updateActivity("logged in", "On Break");
$date = date("m/d/y"); $starttime = time();
$sesh = $_SESSION['fname']." ".$_SESSION['lname'];
$q = "INSERT INTO `breaks` (date, starttime, user) VALUES ('".$date."', '".$starttime."', '".$sesh."')";
$query = mysql_query($q, $conn);
$id = mysql_insert_id($conn);
echo var_dump($id); exit;
edited to show my more recent attempts
Have read all comments given and your replies to each.
Only one of these is possible:
Either the query works properly OR
You are not getting the generated primary key.
Both of these can never be true.
Define, how you know query is working? Do you know the max PK before and after the running query? Is the insert happening from some other place or thread or even other user? the query is working properly from code or from your mysql client?
To diagnose the problem, we have to go though the normal way.
Dump your generated query before calling mysql_query.
Wrap a error checking system around your query call so php can tell you if the query worked or not. I am sure just by these two steps you will realize the root cause of the problem.
error_reporting(E_ALL);
ini_set('display_errors','on');
echo "before calling: $q\n";
$query = mysql_query($q, $conn);
if(!$query)
{
echo "Error:" . mysql_error($conn);
return;
}
echo " generated id:" . mysql_insert_id($conn);
#adelphia as far as i get the idea there is a problem in the query that is executed.
plz check the query properly
Borrow a lead from this code extracted from here:
http://php.net/manual/en/function.mysql-insert-id.php
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>
The problem with your insert query
$q = "INSERT INTO `breaks` (date, starttime, user)
VALUES ('".$date."',
'".$starttime."',
'".$_SESSION['fname'] $_SESSION['lname']."')";
try with this
and main thing you are using most of the deprecated "mysql" things like "mysql_insert_id()"
store the values that u want to pass into an array or variable and pass it in the insert query.
its should work fine then...