A form that populate another form plus a database, it almost works

I’m trying to make a form that will send info to my member database, plus populate the second form so when people don’t have to enter info twice. It almost works. It sends the information to the second form ok. but it only writes a blank record to my member database.Maybe it just can be done. idk
If someone has the time to take a look and possibly help out, I’d be grateful
Here’s what I have. I’ve stripped it down to the bare minimum to make it easier to read

<html>
<head><title>test</title></head>

<?php
if(isset($_POST)){
    // Connection to the database
	include 'connect.php';
    $db_name="members"; // Database name
    $tbl_name="users"; // Table name
	
    // Connect to server and select database.
    $connectDB = mysql_connect("$host", "$username", "$password")or die("cannot connect");
	
    mysql_select_db("$db_name",$connectDB)or die("cannot select DB");
		
	$sql = mysql_query("INSERT INTO users (first_name)
		VALUES('$Username')") or die (mysql_error());
}		
?>

<form id="signup" action="base.php?mode=register" method="post">

<input type="text" tabindex="1" name="username" id="username" size="25" value="JimBob" >

<input type="submit" value="Submit">
</form>

</body>
</html> 

Thanks it advance, Don

You have bigger problems. This code is Obsolete and dangerous junk. You need to toss it. Use PDO with prepared statements. Your code won’t even work at all in a current version of PHP.

Leaving aside the very valid advice* from @benanamen above, in this section of code:

VALUES('$Username')") or die (mysql_error());

where exactly are you setting the value of $Username ? And I’d advise against having $Username for the information you are inserting into the database, and $username for your database connection credentials. Although they are distinct variables as far as PHP is concerned, it is confusing.

Also, what’s the idea of including your connect.php code, but then connecting to the database inside the code you posted? Surely connect.php handles that?

(* - to expand on that - the old-style mysql calls such as mysql_query() are no longer part of the PHP language. If your development environment still supports them, as mine does, you’re running an old version, as I am. If you’re learning PHP, you’d be far better off learning the modern library, which will also make it easier to secure your data. Have a browse down the forum for a few pages, you’ll see loads of examples of PDO and prepared statements.)

All the above may be helpful to some. However, it does not help in solving the problem. As I stated, I’m looking for a way to have a web form do two things at the same time. Junk code? Yes, that may be true, but at 71 years old, I’m not looking to become coding expert. I just want to make this work.

If anyone could show me how to make this work I’d be thankful, if not well, thanks anyway.

How much have you cut out of the code to make it more readable for the forum? That’s a good thing to do, rather than just post everything, but my question was, where do you get the value for $Username from? I can’t see it anywhere in the code, and if you don’t create it from somewhere, then it will have a null value and that’s why your database has empty fields.

I was thinking it would come from the information received from the form input. From your reply, it looks like I may be wrong. Is there something else I need to add here?

Yes. Form variables don’t automatically get assigned variable names like that - if they did, your form defines a field called username, which, if PHP were to just use that for a variable name, would overwrite the variable you use for the database connection credentials.

Form variables will be sent to the page in either the $_GET or $_POST arrays, depending on the method setting in your form definition. You have already seen this as you check for the presence of one of those arrays to decide whether the form has been filled in.

I don’t mind working for my supper :slight_smile:

if (isset($_POST[‘submit’])) {

$username = mysqli_real_escape_string $_POST[‘username’];
}

Thanks

Still, though, don’t forget that you’re using a variable called $username in your database connection string, so if you do intend to use it again for the form variable, make sure you overwrite it at the proper time, i.e. after you’ve done the database connection. And look carefully at the query to make it work with the code you’ve posted above.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.