Unable to enter data into mysql - php

A php script which is intended to enter data into the database is working selectively. Data for 5th and 10th grade gets in but data for all other grades wont show up. I have checked the if else blocks and the logic is working just fine its executing the inner blocks as it is supposed to just wont enter any data into the database. For grades other than 5th or 10th the 2nd block gets executed and i have already checked that it echos the correct value of stuff it is supposed to enter the database. and using a echo after the sql() query i also found out that its working too, so kind of stuck no idea whats wrong please help.
here is the code, let me know if you guys need the html too
<?php
$roll=$_POST['roll'];
$marks=$_POST['marks'];
$dbase=$_POST['std'];
$sec=$_POST['sec'];
$sec1=$_POST['sec1'];
$expire= time()+60;
if (empty($roll) || empty($marks) || ($dbase==0))
{
echo '<p align="center">You did not enter all data. Please go back and enter them.</p>';
echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
die();
}
else
{
if($dbase==5)
{
$temp="five";
}
if($dbase==6)
{
$temp="six";
}
if($dbase==7)
{
$temp="seven";
}
if($dbase==8)
{
$temp="eight";
}
if($dbase==9)
{
$temp="nine";
}
if($dbase==10)
{
$temp="ten";
}
if($dbase==11)
{
$temp="eleven";
}
if($dbase==12)
{
$temp="twelve";
}
if(($dbase==5)&&($sec=="0"))
{
echo '<p align="center">You did not enter all data. Please go back and enter them. 2</p>';
echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
die();
}
else
{
if($dbase<6)
{
$sect=$sec;
setcookie("tab", $temp, $expire, "/","skc-hs.com");
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="data"; // Database name
$tbl_name=$temp; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// To protect MySQL injection (more detail about MySQL injection)
$roll = stripslashes($roll);
$marks = stripslashes($marks);
$roll = mysql_real_escape_string($roll);
$marks = mysql_real_escape_string($marks);
$sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' , '$sect')";
mysql_query($sql);
header( 'Location: dcreate.html');
die();
}
}
if(($dbase!=10)&&($dbase!=5)&&($sec1=="0"))
{
echo '<p align="center">You did not enter all data. Please go back and enter them. 3</p>';
echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
die();
}
else
{
if(($dbase!=5)&&($dbase!=10))
{
$sect=$sec1;
setcookie("tab", $temp, $expire, "/","skc-hs.com");
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="data"; // Database name
$tbl_name=$temp; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// To protect MySQL injection (more detail about MySQL injection)
$roll = stripslashes($roll);
$marks = stripslashes($marks);
$roll = mysql_real_escape_string($roll);
$marks = mysql_real_escape_string($marks);
$sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' '$sect')";
mysql_query($sql);
header( 'Location: dcreate.html');
die();
}
}
if($dbase==10)
{
setcookie("tab", $temp, $expire, "/","skc-hs.com");
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="data"; // Database name
$tbl_name=$temp; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// To protect MySQL injection (more detail about MySQL injection)
$roll = stripslashes($roll);
$marks = stripslashes($marks);
$roll = mysql_real_escape_string($roll);
$marks = mysql_real_escape_string($marks);
$sql="insert into $tbl_name (roll , marks, std) values ('$roll' , '$marks' , '$dbase')";
mysql_query($sql);
header( 'Location: dcreate.html');
die();
}
}
?>
Structure of table(expect for the table "ten")
Column Type Collation Attributes Null Default ExtraAction
id int(11) No None AUTO_INCREMENT
roll varchar(3) latin1_swedish_ci No None
marks smallint(6) No None std varchar(2) latin1_swedish_ci No None
sec varchar(1) latin1_swedish_ci No None
Structure of table ten
Column Type Collation Attributes Null Default ExtraAction
id int(11) No None AUTO_INCREMENT
roll varchar(3) latin1_swedish_ci No None
marks smallint(6) No None std varchar(2) latin1_swedish_ci No None
when i modify the 2nd if block like this
if(($dbase!=5)&&($dbase!=10))
{
$sect=$sec1;
echo "roll- ";
echo $roll;
echo " marks- ";
echo $marks;
echo " std- ";
echo $dbase;
echo " section- ";
echo $sect;
setcookie("tab", $temp, $expire, "/","skc-hs.com");
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="data"; // Database name
$tbl_name=$temp; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// To protect MySQL injection (more detail about MySQL injection)
$roll = stripslashes($roll);
$marks = stripslashes($marks);
$roll = mysql_real_escape_string($roll);
$marks = mysql_real_escape_string($marks);
$sql="insert into $tbl_name (roll , marks, std, sec) values ('$roll' , '$marks' , '$dbase' '$sect')";
mysql_query($sql);
echo " done";
die();
}
it echos "roll- 12 marks- 454 std- 7 section- B done" when i enter those values in the html form. I dont get it why it won't just enter those values in the database

You have forget to place a comma betwwen $dbase and $sect in query.
Change the query from
$sql="insert into $tbl_name (roll , marks, std, sec)
values ('$roll' , '$marks' , '$dbase' '$sect')";
to
$sql="insert into $tbl_name (roll , marks, std, sec)
values ('$roll' , '$marks' , '$dbase' , '$sect')";

You seem to exit() the script if anything isn't a 10 or a 5 with this code:
if(($dbase!=10)&&($dbase!=5)&&($sec1=="0"))
{
echo '<p align="center">You did not enter all data. Please go back and enter them. 3</p>';
echo '<FORM><p align="center"><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></p></FORM>';
die();
}

Related

MYSQL Create Table Error "A table must have 1 Column"

I am creating a MYSQL Table in my database using php:
$sql="CREATE TABLE $URL(Image BLOB,Rating INT(255),Id INT KEY AUTO_INCREMENT)";
However, it errors out and says: A table must have at least 1 column.
Why is it doing this?
Here is the full php file used to create the table:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="questapic"; // Database name
$tbl_name="tabledirector";
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
// get data that sent from form
$TN = mysql_real_escape_string($_POST['name']);
$TQ = mysql_real_escape_string($_POST['q']);
$CR = mysql_real_escape_string($_POST['creator']);
$datetime=date("d/m/y"); //create date time
$R1=rand(5000, 15000000);
$R2=rand(5000, 15000000);
$R3=rand(5000, 15000000);
$URL = $TN . $R1 . $TQ . $R2 . $CR . $R3;
$URL=str_replace(" ","#%","$URL");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="INSERT INTO $tbl_name(URL,topic)VALUES('$URL','$TQ')";
$result=mysql_query($sql);
$sql="CREATE TABLE $URL(Image BLOB,Rating INT(255),Id INT KEY AUTO_INCREMENT)";
$result=mysql_query($sql);
if (!$result) die (mysql_error());
mysql_close();
header("location:Your_Special_Code_Is.php?id=$URL");
?>
Try:
$sql="CREATE TABLE ".$URL."(Image BLOB,Rating INT(255),Id INT KEY AUTO_INCREMENT)";

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.

number format in SQL INSERT

Hi guys i need some support on number format in SQL INSERT INTO
i need to convert {$data['rank']['points']} as number format, so that instead of it showing as 5467389 it converts it to 54,673,89
the data is being imported from API Data
this is my code
$host="hostname"; // Host name
$username="database"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="table"; // 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");
// API Data
$url = "http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80";
$json = $json = file_get_contents($url);
$data = json_decode($json, true);
date_default_timezone_set('Europe/London');
// Insert data into mysql
$sql="INSERT INTO $tbl_name(citid, citname, rankpoints)VALUES('{$data['id']}', '{$data['name']}', '{$data['rank']['points']}')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='supplies_insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
Try this:
$query = sprintf("INSERT INTO `%s` (citid, citname, rankpoints) VALUES ('%s', '%s', '%s')",
mysql_real_escape_string($tbl_name),
$data['id'],
mysql_real_escape_string($data['name']}),
mysql_real_escape_string(number_format($data['rank']['points'])));
$result = mysql_query($query);
You can use php's number_format() function if I understand your requirement correctly. Let me know if that's not what you want.

How to Reset Counter when 5 is reached

can someone help me with a reset for a mysql counter. here is the code
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counter'];
// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// count more value
$addcounter=$counter+1;
// reset counter if 5 has been reached
If (counter==5){
echo "counter=5 ";
// now im getting an error here//
counter=0;
}
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);
mysql_close();
?>
the error is:
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\test\counter.php on line
and based on // counter=0
you forgot $:
$counter=0;
counter on it's own is not a variable, additionally in your update query, you most likely after 0 value if you reached 5
If ($counter==5){
echo "counter=5 ";
$counter=0;
$addcounter=0;
}
this is the corrct working code.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="gametest"; // Database name
$tbl_name="counter"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$counter=$rows['counter'];
// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}
echo "You 're visitors No. ";
echo $counter;
// count more value
$addcounter=$counter+1;
// reset counter if 5 has been reached
If ($counter==5){
$counter=0;
$addcounter=0;
}
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);
mysql_close();
?>

Categories