Jump to content

Taking values of variable from forms


coco243

Recommended Posts

Hellow, I have the following issue if you please can help me. I have this index.php file:

<html><body><h1>It works!</h1><form action="/PHP/update.php" method="post">Age: <input type="text" name="age"><input type="submit"></form><?php//connecting to database//$con=mysqli_connect("localhost","root","gogonea","my_db"); // conection checking//if (mysqli_connect_errno())//{// echo "conection failed ".mysqli_connect_error();//}//else//{//echo "Ghiata, conection established! <br>";//} function afi(){include("displaytable.php");}//afi();  //closing connection//mysqli_close($con);?><div><p>cucu</p><form name="displaytable" action="\PHP\displaytable.php" method="get"><input type="submit" value="Display Table"></form> </div></body></html>

And this update.php file:

<?php//connecting to database$con=mysqli_connect("localhost","root","gogonea","my_db"); //conection checkingif (mysqli_connect_errno()){  echo "conection failed ".mysqli_connect_error();}else{echo "Ghiata, conection established! <br>";} //Updating Tablemysqli_query($con,"UPDATE Persons SET Age=ageWHERE FirstName='Peter' AND LastName='Griffin'"); //closing connectionmysqli_close($con); ?>

I can't update the age, taked from the form scripted in the index.php This is a test of how to take values from forms and transmit them to a database. If I wasn't exprimed clear please tell me to be more explicite if is the case. Thank you verry much

Edited by damy
Link to comment
Share on other sites

It's not working.When i display table the age value is the same as it was.I don't have experience, and I can't figure where is the problem. I have to modify something at the form script from index.php also?

Edited by damy
Link to comment
Share on other sites

I had taked the information taken from that form ( age parameter )

<form action="/PHP/update.php" method="post">Age: <input type="text" name="age"><input type="submit"></form>

And introduced and I had introduce it on a variable:

$x=$_POST["fage"];

and dyspayed it here.

echo $x;

It works. Also I had verified if my updating STATMENT IS OK, and I had placed the number 20 at Age parameter, and when I had dysplayed the table, it was apearing the modification, so the UPDATE statment is OK. The next step was to pass the value of submit to variable x, and than to the UPDATE statament:

mysqli_query($con,"UPDATE Persons SET Age=$xWHERE FirstName='Peter' AND LastName='Griffin'");

Didn't worked, why?

Edited by damy
Link to comment
Share on other sites

$query = mysqli_query($con,"UPDATE Persons SET Age=$x WHERE FirstName='Peter' AND LastName='Griffin'");if(!$query) {    echo mysqli_error();}

I think, if Age is a text field, that you should be putting the value of $x between quotation marks:SET Age='$x'

Link to comment
Share on other sites

And the indication of kanchatchai is working, I think I was putting ";" after SET=".$_POST['age']." or after SET=x, and what was following ";" wasn't taked for compiling. Thank you all for help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...