Reputation: 2688
I am trying to bring a value from a form in page1.php (form to add data to DB) to page2.php (form to add IMG to DB). I read a whole bunch of pages about how to do that but still can't figure it out.
<form name="formBrokerTour" class="login-form-addproperty" method="post"
action="insertPropertyInfoWITHgeocode.php" onsubmit="return validateForm()">
And then I would need to add several fields, address, city, zip code state. Here one out of 4:
<td width="125" class="formLable">Address</td>
<td width="269" class="formSquare">
<input type="text" class="general_input"
name="address" id="nameText" width="269" >
</td>
page2.php (relevant part) - as you see, if I write an address, that address is addded in the database, so we don't need to worry about sql being wrong. If I try to pass info from page 1 to page 2 either by session (since I have username in place to add info in database) or post/get, address is not added. Interestingly, username is being added.
$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
$address = "123 abec st";
$query = "
INSERT INTO my_image
(name, size, type, file_path, username, address)
VALUES(?,?,?,?,?,?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
$conn->bind_param("sissss", $myfile, $fileSize, $fileType, $path, $username, $address);
if (!$conn->execute()) {
echo 'error insert';
} else {
echo 'Success!<br/>';
echo '<img src="' . DISPLAY_PATH . $myfile . '"/>';
}
}
I tried to echo $_session
, etc. as we see in other questions, but here is the problem of a new computer person... where do I add this info? I have added it in session_start
on the top, just before SQL query and it didn't accept it inside the query.
page1.php has submit button with DB and geocoding stuff. It sends to page3.php where it asks if user wants to add images. If yes, page2.php, if not index. I am getting all confused with passing info.
Upvotes: 1
Views: 1817
Reputation: 2688
For those struggling with the same question: I added this in all related files
<?php session_start();
$_SESSION['address']=$address;
$_SESSION['city']=$city;
$_SESSION['zip_code']=$zip_code;
?>
I added this before the query:
$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
$address= htmlentities($_SESSION['address']);
$city= htmlentities($_SESSION['city']);
$zip_code= htmlentities($_SESSION['zip_code']);
Then the query:
$query =
"INSERT INTO my_image(name, size, type, file_path, username, address, city, zip_code) VALUES(?,?,?,?,?,?,?,?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
$conn->bind_param("sissssss", $myfile, $fileSize, $fileType, $path, $username, $address, $city, $zip_code);
.......etc.
There may be a little bit of magic, I mean, maybe some steps aren't needed. It's working after trying for a long long time.
Upvotes: 0
Reputation: 1188
Because you're using ?
instead of variables in your query, the correct usage of bindParam is like $conn->bindParam(number_of_param, the_value);
where number_of_param is the number of the ?
you are replacing.
You can also pass an array of parameters to the $conn->execute()
call as shown below to replace all the ?
's at once:
$conn = $db->prepare("INSERT INTO my_image(name, size, type, file_path, username, address) VALUES(?,?,?,?,?,?)");
if ($conn == TRUE) {
$result = $conn->execute(array($myfile, $fileSize, $fileType, $path, $username, $address));
if (!$result) {
echo 'error insert';
} else {
echo 'Success!<br/>';
echo '<img src="' . DISPLAY_PATH . $myfile . '"/>';
}
}
side note: I would recommend using the PDO::EXCEPTION style of error handling and surrounding your database code with a try {} catch
block, as it can help with standardizing error handling, but this is completely up to you.
Upvotes: 1
Reputation: 446
use
$_POST['form input name']
so
VALUE ($_POST['myfile'], $_POST['fileSize'],ect.)
Upvotes: 0
Reputation: 23011
All of the fields from your form are in $_POST. You would extract the address with
$address = $_POST['address'];
If you were passing the variables with a GET form action or via the URL, you would use $_GET.
I'd really suggest looking at tutorial sites or a PHP book, as this is one of the basic things you would learn.
Upvotes: 3