Insert statement not working. transfer from one table to another - php

Pretty new to PHP and MySQL.
I have created an insert statement in my php script, to transfer a row of data from one table to the next for certain fields. Only thing is, it doesn't seem to be working?
Can anybody see where the issue is?
<?php
require_once('auth.php');
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Instruction"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Reference=$_REQUEST['Reference'];
$Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];
$DOB=$_REQUEST['DOB'];
$Mobile=$_REQUEST['Mobile'];
$Home=$_REQUEST['Home'];
$Address=$_REQUEST['Address'];
$Postcode=$_REQUEST['Postcode1'];
$Email=$_REQUEST['Email'];
$Accident=$_REQUEST['Accident'];
$Details=$_REQUEST['Details'];
//semi colon removed
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details)
VALUES('.$Reference.','.$Forename.','.$surname.','.$DOB.','.$Mobile.','.$Home.','.$Address.','.$Postcode1.','.$Email.','.$Accident.','.$Details.')";
$result=mysql_query($sql);
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
?>

You have column names like Mobile Numbe etc, you need to use `` for them also the concatenation does not look correct you should have something as
sql="INSERT INTO
Triage
(
Reference,
Forename,
surname,
`D.O.B`,
`Mobile Number`,
`Home Number`,
Address,
Postcode1,
Email,
Accident,
Details
)
VALUES
(
'".$Reference."',
'".$Forename."',
'".$surname."',
'".$DOB."',
'".$Mobile."',
'".$Home."',
'".$Address."',
'".$Postcode1."',
'".$Email."',
'".$Accident."',
'".$Details."'
)";
In addition you should use mysql_real_escape_string() for all the request data something as
$Reference=mysql_real_escape_string($_REQUEST['Reference']);
and so on for others

Related

Cannot connect to a database in wamp

I created a simple form in HTML to add the details of student. It contains text fields foe first name, last name, age, phone and email. And i created a database in wamp phpmyadmin and created a table named stud with fields fname, lname, age, phone, email and my PHP code to add the data from the form to table stud is given below
<?php
$host="localhost";
$username="";
$password="";
$db_name="student";
$tbl_name="stud";
$connect=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($connect,$db_name)or die("cannot select DB");
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$age=$_POST['age'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$sql="INSERT INTO $tbl_name(fname, lname, age, phone, email)VALUES('$fname', '$lname' ,'$age', '$phone', `'$email')";`
$result=mysqli_query($connect,$sql);
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='student.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
mysqli_close($connect);
?>
when I run this code in a browser it shows "cannot connect DB".
But when i created a table named stud in test database it worked.
Please help me to find a way to connect to my stud table in student database in wamp.
You can also use below
$connect=mysqli_connect("localhost","username","password","db_name")or die("Error in database connection");
OR
Check your username beacause default username in wamp is
root
There is an error in your code. use
mysqli_select_db($db_name, $con) instead of
mysqli_select_db($con, $db_name)

Insert statment only insert one field into new table

I'm having a issue with an insert statement. It should transfer a row of data from one table to another upon clicking a link from a table from that specified row.
At the minute the only field being inserted into the other table is the reference.
In other words I want to retrieve a person's name and details from a row in one table which corresponds to a unique reference number and move it to the next table in the process.
Table Page.php
<td align="center">Refer for Triage</td>
<td align="center">Refer for IA</td>
Script.php - action page
require_once('auth.php');
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="sysadmin"; // Mysql password
$db_name="Elite"; // Database name
$tbl_name="Instruction"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Reference=$_REQUEST['Reference'];
$Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];
$DOB=$_REQUEST['DOB'];
$Mobile=$_REQUEST['Mobile'];
$Home=$_REQUEST['Home'];
$Address=$_REQUEST['Address'];
$Postcode=$_REQUEST['Postcode1'];
$Email=$_REQUEST['Email'];
$Accident=$_REQUEST['Accident'];
$Details=$_REQUEST['Details'];
//semi colon removed
$sql="INSERT INTO
Triage
(
Reference,
Forename,
surname,
`D.O.B`,
`Mobile Number`,
`Home Number`,
Address,
Postcode1,
Email,
Accident,
Details
)
VALUES
(
'".$Reference."',
'".$Forename."',
'".$surname."',
'".$DOB."',
'".$Mobile."',
'".$Home."',
'".$Address."',
'".$Postcode1."',
'".$Email."',
'".$Accident."',
'".$Details."'
)";
$result=mysql_query($sql);
// echo "Successful";
// echo "<BR>";
// echo "<a href='list_records.php'>View result</a>";
// mysql_error()
echo $sql
?>

Insert statement not working. not transferring into other table

Pretty new to PHP and MySQL.
I have created an insert statement in my php script, to transfer a row of data from one table to the next for certain fields. Only thing is, it doesn't seem to be working?
Can anybody see where the issue is?
<?php
require_once('auth.php');
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Instruction"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details);
VALUES (Reference,Forename,surname,DOB,Mobile,Home,Address,Postcode1,Email,Accident,Details)";
$result=mysql_query($sql);
//
while($rows=mysql_fetch_array($result)){
echo 'update test';
}
//
// end of while loop
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
?>
Update
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Reference=$_REQUEST['Reference'];
$Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];
$DOB=$_REQUEST['DOB'];
$Mobile=$_REQUEST['Mobile'];
$Home=$_REQUEST['Home'];
$Address=$_REQUEST['Address'];
$Postcode=$_REQUEST['Postcode1'];
$Email=$_REQUEST['Email'];
$Accident=$_REQUEST['Accident'];
$Details=$_REQUEST['Details'];
//semi colon removed
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details)
VALUES('.$Reference.','.$Forename.','.$surname.','.$DOB.','.$Mobile.','.$Home.','.$Address.','.$Postcode1.','.$Email.','.$Accident.','.$Details.')";
$result=mysql_query($sql);
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
?>
first you should fix the assignments:
$Reference=$_REQUEST['Reference'];
$Reference=$_REQUEST['Forename'];
...
should be something like:
$Reference=$_REQUEST['Reference'];
$Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];
Then update the query in:
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details)
VALUES (".$Reference.",".$Forename.","...
and so on with the rest of the values.
Also
while($rows=mysql_fetch_array($result)){
won't work since result will only contain true on success.
Maybe there are more mistakes I'm not sure. But you should also check this to learn how to avoid injection:
What's the best method for sanitizing user input with PHP?
If you want to transfer data from one table to another, you should select this table somewhere. You have not anywhere in your code, you just specified columns, how is your script supposed to know where do they come from?
INSERT INTO table1 (col1, col2, col3) SELECT correspondingColumn1, correspondingColumn2, correspondingColumn3 FROM table2
P.S.: You do not use $Reference, but still, you are overwritting it
try this one
1) you mention all var name as $Reference its changed
2) query not correct plz study how wrote query..
3) REFER:http://www.w3schools.com/php/php_mysql_intro.asp
<?php
require_once('auth.php');
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="Instruction"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$Reference=$_REQUEST['Reference'];
$Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];
$DOB=$_REQUEST['DOB'];
$Mobile=$_REQUEST['Mobile'];
$Home=$_REQUEST['Home'];
$Address=$_REQUEST['Address'];
$Postcode=$_REQUEST['Postcode1'];
$Email=$_REQUEST['Email'];
$Accident=$_REQUEST['Accident'];
$Details=$_REQUEST['Details'];
//semi colon removed
$sql="INSERT INTO Triage (Reference,Forename,surname,D.O.B,Mobile Number,Home Number,Address,Postcode1,Email,Accident,Details)
VALUES ('$Reference','$Forename','$surname','$DOB','$Mobile','$Home','$Address','$Postcode1','$Email','$Accident','$Details')";
$result=mysql_query($sql);
//
while($rows=mysql_fetch_array($result)){
echo 'update test';
}
//
// end of while loop
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
?>

Hostgator SQL database not updating with PHP form

I have a PHP form that should insert data into my SQL database on hostgator. However it is not adding any data but the id field keeps incrementing. I do not receive any error message when submitting the form and when i go to the database the other fields are just empty thus not displaying any data.
I am pulling my hair and cant figure out what the problem is. Can someone please help me
Thanks
<?php
$host="localhost"; // Host name
$username="xxxxxx"; // Mysql username
$password="xxxxxx"; // Mysql password
$db_name="rob1124_inventory"; // Database name
$tbl_name="data"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$qty=$_POST['qty'];
$product=$_POST['product'];
$price=$_POST['price'];
$totalprice=$_POST['totalprice'];
$seller=$_POST['seller'];
$city=$_POST['city'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(qty, product, price, totalprice, seller,city)
VALUES('$qty', '$product', '$price', '$totalprice', '$seller', '$city')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
Change to utf-8 from all varchar fields of your table and
try to get mysql_error().
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("set names 'utf8'");
//You codes....
// Insert data into mysql
$sql="INSERT INTO $tbl_name(qty, product, price, totalprice, seller,city)
VALUES('$qty', '$product', '$price', '$totalprice', '$seller', '$city')";
$result=mysql_query($sql) or die(mysql_error());
//Your codes...
Since the id is incrementing atleast the form and the DB connect, it tries to enter data.
One usually occurring error is that the data types in the databases columns don't match with the type of data recieved. Like trying to insert chars into ints etc. Or the length of the data is to large for the assigned size in the database. Check to see that the types are correct and try again.
But still, those that are correct should be inserted. Hard to tell without knowing more about the database design.

'Query was empty' error when pushing form data to Database PHP, MYSQL

The code I am using (below) doesn't work for some reason, I check everything matches in the html form - I just can't figure this out.
Could I be getting the error because something that is wrong on the HTML side? I'm using the jQuery form validator (this works fine) could this cause problems?
http://jqueryvalidation.org/
<?php
$host="localhost"; // Host name
$username="USERNAME"; // Mysql username
$password="PASSWORD"; // Mysql password
$db_name="DBNAME"; // Database name
$tbl_name="Persons"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$company=$_POST['company'];
$email=$_POST['email'];
$registered=$_POST['registered'];
$percentage=$_POST['percentage'];
$products=$_POST['products'];
$prize=$_POST['prize'];
$terms=$_POST['terms'];
$newsletter=$_POST['newsletter'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
$result = mysql_query($query) or die(mysql_error());
?>
<?php
// close connection
mysql_close();
?>
Your getting the error on
<?php
$result = mysql_query($query) or die(mysql_error());
?>
Which is executed later down your script and $query is not set. Your real insert is way up above.
$sql="INSERT INTO $tbl_name(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);
You should also change that to $result=mysql_query($sql) or die(mysql_error());
and see if there is errors on the actual insert. remove the secondary query execution.
$sql="INSERT INTO $tbl_name VALUES(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);

Categories