I have a website which has a form which submits the data to a database and also sends an email to an email address, which has a random id at the end of the URL which will pull the specific data and display it on a web page. Basically its a valentines message type thing I’m doing
I’ve got that the data being sent to the database and storing perfectly but I’m having issues with pulling the data for some strange reason.
Here’s my code, is there anything you can see that might be causing the issue?
<?php
// since the id is being passed in the url, you will need to declare it using the get method
$rand = $_GET['rand'];
$action = $_GET['action'];
// if an id was sent to the script, then execute it
if ($rand)
{
// connection vars
$host = "localhost";
$user = "***";
$password = "***";
$dbname = "***";
$tablename = "cards";
$mysql = new mysqli('$host, $user, $password');
$result = $mysql->query('SELECT * FROM $tablename WHERE rand = $rand');
while (($row = $result->fetch_assoc()) !== null) {
print_r($row);
$youremail = urlencode($row['youremail']);
$name = urlencode($row['name']);
$receiveremail = urlencode($row['receiveremail']);
$message = $row['message'];
// replace non flash line breaks with the flash \r newline
$message = str_replace('\n', '\r', $message);
$message = str_replace('\r\n', '\r', $message);
$message = str_replace('<br>', '\r', $message);
$message = str_replace('%0D%0A', '\r', $message);
}
// if there was a result echo the stuff below
if($result)
{
// if we have a result we can show the movie and pass the vars along in the strings
// a set back with this is that you can only pass so much data in the string, think its like 256 characters, but Im not sure.
echo "Hello, $name <br />";
echo "$message";
exit();
}
mysql_close();
}
?>
I would change it to the code below.
Changed your
isset, yourwhile ...loop and removed your false check on$result. I also removed the calls tourlencodeyou were using, since they do not make any sense if you are going to display these values on the screen. That function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page. See here.Lastly, get rid of those quotes in your
new mysqli()call.