I want to update a table on a specific row need some advice on my php statement
I use this statement to call the client's info
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="members"; // 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 value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE member_msisdn='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
This works fine echoĆng the information I need and able to alter it.
<form name="form1" method="post" action="control_clientupdated.php">
This referes to my action php script
Problem I need assistance with is how do i notify my action script to use the same id I ran the query on to update that row.
// 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");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
I have placed the WHERE statement on the end of the update
Let me just state it shows done, but it did not update the table at all
Firstly you need to store your ID into a hidden form element in your form.
<form method="post" action="control_clientupdated.php">
<input type="hidden" name="member_msisdn" value="<?=$query?>" />
...
</form>
This will allow you to passthrough the value from your first php script.
Then in your control_clientupdated.php you need to use $_POST to recover your value.
// Store the $_POST value for my query ID
$query = $_POST['member_msisdn'] ;
// 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");
// update data in mysql database
$sql="UPDATE $tbl_name SET member_name='$member_name',
member_surname='$member_surname', member_msisdn='$member_msisdn', cid='$cid',
cofficenr='$cofficenr', cfax='$cfax', e2mobile='$e2mobile' WHERE member_msisdn='$query'";
$result=mysql_query($sql);
This should be what you need - note that you cannot use $_GET to retrieve the variable passed by the form, as you are sending it with the method="post" attribute, you must use $_POST instead of $_GET
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
Hi i need some help in coding php to connect database
my source code is
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name
// Connect to server and select databse.
`mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`
But it display error
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\123\tryforum\main_forum.php on line 11
cannot select DB
How to solve it
You need to pass connection variable to mysqli_select_db.
See this link
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_question"; // Table name
// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password") or die("cannot connect");
mysqli_select_db($con, "$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";`
mysqli_select_db()
needs two parameter first your database connection and second your
database name
$conn=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($conn,"$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysqli_query($conn,$sql);
I'm having a little problem with php, basically I want to get a random row from my mysql database, I am really new to php and mysql so please be kind and explain me what's going on. I've already granted all permissions on mysql, now I just have to figure out what's going on, i tried to put some echoes to debug but it seems like anything happens, there's just a blank page with nothing on it, this drives me crazy so I'd like to resolve it. Here's the code
<?php
echo "test";
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // 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");
// Select a random account
$min=1;
$row=mysql_fetch_assoc(mysql_query("SHOW TABLE STATUS LIKE 'mine.accounts';"));
$max=$row["Auto_increment"];
$random_id=rand($min,$max);
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
echo $row["username"]. ":" . $row["password"]
?>
// --- UPDATE ---
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$host="127.0.0.1"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="mine"; // Database name
$tbl_name="accounts"; // 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");
// Select a random account
$row = mysql_query("SELECT username AND password FROM accounts order by RAND() LIMIT 1");
WHILE ($data = mysql_fetch_array($row))
ENDWHILE;
echo $row['username'] . " " . $row['password'];
?>
On this line, you forgot the closing parentheses.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'");
Hence the single closing parentheses while you open two.
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM `mine`.`accounts` WHERE id='$random_id'"));
And you'll have to use a while loop to make $row output anything, since fetch_assoc returns an associative array:
while($row = mysql_fetch_assoc(<...>){
$max = $row['Auto_increment'];
}
Also you might wanna look into Prepared Statements or PDO as mysql_* Functions are officially deprecated.
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>";
?>
I'm having an issue with a SQL update query, it says successful but doesn't actually update the database record.
<?php
require_once('auth.php');
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="sysadmin"; // Mysql password
$db_name="Elite"; // Database name
$tbl_name="Triage"; // Table name
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="UPDATE Triage SET directly='$directly', psychologically='$psychologically' WHERE Reference='$Reference'";
$result=mysql_query($sql);
$sql="UPDATE Triage SET directly='$directly', psychologically='$psychologically' WHERE Reference='$Reference'";
none of the variables used in that query have been defined in the code above. $directly,$psychologically,$Reference: none have a value. Define values for those and that's it.
That $Reference is a must, even If others aren't.
$refrence="1";
$sql="UPDATE Triage SET directly='$directly' AND psychologically='$psychologically' WHERE Reference='$Reference'";
replace $refrence with your MySQL ref
Use this syntax
$sql= UPDATE Triage SET directly=?, psychologically=? WHERE Reference=?";
$param->execute(array($directly,$psychologically,$Reference));
$result=mysql_query($param);
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.