How to Reset Counter when 5 is reached - php

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();
?>

Related

PHP wont delete mysql rows

Everything seems OK, but it won't delete rows. MySQL version is 5.1. Script is:
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$db = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "DELETE FROM oc_ixml_cat_map WHERE name='***'";
$result=mysql_query($sql,$db) or die(mysql_error());
if ($result) {
echo "OK";
}
else {
echo "Not OK";
}
?>
Try to change the string to:
DELETE FROM oc_ixml_cat_map WHERE id=1
Where 1 is an available ID of your table just to see if the command is running or it is connection problem...

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>";
?>

Unable to enter data into mysql

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();
}

Simple database lookup - keeps looping

I have the following code running on my website. I call it from a html form. The form has a client number which looks up the redirect value in the database and should redirect to the website. The redirect works great with one value in the database. However, when I add more values it uses the last one. I added the echo goto so I can see. The script pulls every redirect value for the whole database. How can I just pull the redirect value associated with the client_number?
<php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="clients"; // 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");
// sent from form
$myclient=$_POST['myclient'];
$myclient = stripslashes($myclient);
$sql="SELECT * FROM $tbl_name WHERE client_number='$myclient'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
$result = mysql_query("SELECT redirect FROM clients");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$goto = $row['redirect'];
//added to see output
echo $goto;
//header ("location: $goto");
}
exit();
}
else {
echo "Account number is invalid";
}
?>
From your query you are pulling all redirect
$result = mysql_query("SELECT redirect FROM clients");
which should be
$result = mysql_query("SELECT redirect FROM clients WHERE clientID=1234");
Then you were using while instead of if
if($row = mysql_fetch_array($result, MYSQL_ASSOC)) { }
Using header('Location: xyz.ext') redirect in loop is bad

Categories