URLdata is present. Moving it to the db is a problem

Hi.
I'm losing my mind here. I've put in countless days on this one issue alone and at one point probably did it right but a battery drained laptop, lack of sleep and touching keys, and or just plain inexperience has held me in stasis for the time being. I designed and built the front end of a website from scratch for my business. Its probably 83% of being perfected, but I need to work and as long as I can't move through this simple task, its just an overgrown paperweight. Can someone please help.

The issue is this. I have connection to the database. I see the information that i want to insert into the database in the url but for some reason, the code doesn't see it and nothing ends up in the database. If I can get through this issue, I can finish the contact and registration forms, as well as the signup login mechanism.

There's lots of old videos and a few that move way to fast for me. I attempted mysql, mysqli, prepared statements but this method finally put air into the database. Thanks for your help.

<?php $servername = "localhost"; $username = "root"; $password = "rootagain"; $dbname = "rootforthehometeam";
$db_conx = new mysqli("localhost", "root", "rootagain", "rootforthehometeam");

$level = ($_POST['level']); 
$co = ($_POST['co']);
$fname = ($_POST['fname']);
$lname = ($_POST['lname']);
$mailfrom = ($_POST['mailfrom']);
$emailtopic = ($_POST['emailtopic']);
$message = ($_POST['message']);

$sql = "INSERT INTO form_contact (level,co,fname,lname,mailfrom,emailtopic,message)
VALUES ('$level','$co','$fname','$lname','$mailfrom','$emailtopic','$message')";

$mailmessage = 'You have a message from: '.$fname.' '.$lname.' for' .$emailtopic;

$to = 'Directed to: '.$emailtopic;
$headers = 'From: '.$mailfrom.'\n';
$headers .= 'MIME-Version: 1.0\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1\n';

mail($to, $emailtopic, $message, $headers);
header('Location: https://www.minniecooperdrivesher.com');
mysqli_close($db_conx);

?>

In a nutshell, you have absolutely no code to execute the query. As long as your learning you should really be using PDO.

Additionally, stop creating variables for nothing, use Prepared Statements, and let Php handle the closing of the DB connection.

2 Likes

But you still just defined an SQL-string that you do not execute. You’re just opening and closing the connection, but not actually using it.

1 Like

Just have a look at the link provided by @benanamen, there are pretty easy to understand examples.

1 Like

The first issue that blocked anything from going into the database was the code for the email. So I separated it and the page. The database received instantly from there. I’ll update my form page to send to a page that sends to the database and one that sends an email response.

As for my original issue, the form still misses the data from the form and instead populates the database with the variable names. Example, inside the table the columns would read as level, co, fname, lname, mailfrom, message and the values would also be the same.
The url will show ?level=guest&?co=whatever and so on and so forth.

If you can assist on that, great, very much appreciated.

<?php //added to attempt to pull info from url or wherever its floating around// $level = ($_POST['level']); $co = ($_POST['co']); $fname = ($_POST['fname']); $lname = ($_POST['lname']); $mailfrom = ($_POST['mailfrom']); $emailtopic = ($_POST['emailtopic']); $message = ($_POST['message']); $servername = "localhost"; $username = "root"; $password = "rootroot"; $dbname = "rootforthehometeam"; // Create connection $db_conx = new mysqli("localhost", "root", "rootroot", "rootforthehometeam"); // Check connection if ($db_conx->connect_error) { die("Connection failed: " . $conn->connect_error); } // prepare and bind $stmt = $db_conx->prepare("INSERT INTO form_contact (level, co, fname, lname, mailfrom, emailtopic, message) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("sssssss",$level, $co, $fname, $lname, $mailfrom, $emailtopic, $message); // set parameters and execute $level = 'level'; $co = 'co'; $fname = 'fname'; $lname = 'lname'; $mailfrom = 'mailfrom'; $emailtopic = 'emailtopic'; $message = 'message'; $stmt->execute(); echo "New records created successfully"; $stmt->close(); $db_conx->close(); ?>

Your URL is broken. The questionmark is once and only at the beginning, follwed by the first key/value, then every other variable pair comes after an ampersand.

You are requesting POST parameters where your form provides GET parameters. Check the “method” attribute of your form.

Prepared Statements

1 Like

I changed the get to method=“post” in the index page thanks. The url was typed that way by me from memory but that wasn’t the way it really was. So, sorry about that. I have the page already done in mysqli prepared statements and it puts data into the database, but like i said earlier, its not the data itself. The change from get to post may fix that, i hope. i updated the entire topic in a new one out of frustration. Thank you for your input here.

It reads as "Mysqli Insert data into database issue.

Thanks again for your time

Hello to insert data from url you should use $_GET method.
And pdo is simple and it makes things very simple, take look at this file

1 Like

@tp45, While PDO is the right direction, the code you linked to has several flaws.

Basically I can’t access the data that was sent from the form page to the parser page to go into the database for some unknown reason to my eyes. The form data is present in the url, but the values in the variables are not updating as so.

for example;
url show fname = bobsdiscounts

this was then used as a “maybe this will work moment”:
$fname = ($_POST[‘fname’)];
$fname = ‘fname’; (to pull the data)

the entire code is this:

<?php
	$level = ($_POST['level']); 
	$co = ($_POST['co']);
	$fname = ($_POST['fname']);
	$lname = ($_POST['lname']);
	$mailfrom = ($_POST['mailfrom']);
	$emailtopic = ($_POST['emailtopic']);
	$message = ($_POST['message']);
	$servername = "localhost";
	$username = "root";
	$password = "rootroot";
	$dbname = "rootforthehometeam";

// Create connection
	$db_conx = new mysqli("localhost", "root", "rootroot", "rootforthehometeam");

// Check connection
if ($db_conx->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $db_conx->prepare("INSERT INTO form_contact (level, co, fname, lname, mailfrom, emailtopic, message) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss",$level, $co, $fname, $lname, $mailfrom, $emailtopic, $message);

// set parameters and execute
	$level = 'level'; 
	$co = 'co';
	$fname = 'fname';
	$lname = 'lname';
	$mailfrom = 'mailfrom';
	$emailtopic = 'emailtopic';
	$message = 'message';
	$stmt->execute();

	echo "New records created successfully";

	$stmt->close();
	$db_conx->close();
?>
  • Admin Edit: Added Code Formatting Tags.

then you are obviously still using GET in your form, but require POST data in your script.

Sponsor our Newsletter | Privacy Policy | Terms of Service