Jump to content

Taking values of variable from forms

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 post
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 post
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:


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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...