Jump to content

getting started with PHP/the easiest question in this forum (probably)


Recommended Posts

Hi,

 

I'm trying to run this script (the values come from an HTML form on another page, I put that code below):

 

<?php
$username="xxx";
$password="xxx";
$database="xxx";

$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO employees VALUES ('','$firstName','$lastName')";
mysql_query($query);

mysql_close();
?>

 

On my actual script, I've got my username/pw/etc filled in, so that's not it... am I missing something else really simple? All this yields is a blank page. When I look at the employees table w/ MySQL Query Browser, I see that no entries are added.

 

Here's my HTML form...

<form action="insert.php" method="post">
First Name: <input type="text" name="firstName"><br>
Last Name: <input type="text" name="lastName"><br>
<input type="Submit">
</form>

 

On the same site, I setup a Phorum (PHP/MySQL based) message board that works with my database, so I'm sure that my server is setup properly.

 

I know this is ridiculously basic, but it's been driving me crazy for at least a week now and, again, help is extremely appreciated.  If you live in Minnesota, I'll buy you a beer.

It should be blank after you hit submit because your .php page doesn't do anything after the insert.

 

Also, change

 

mysql_query($query);

 

to

 

mysql_query($query) or die (mysql_error());

 

And report the error.

 

 

 

Hi,

 

I'm trying to run this script (the values come from an HTML form on another page, I put that code below):

 

<?php
$username="xxx";
$password="xxx";
$database="xxx";

$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO employees VALUES ('','$firstName','$lastName')";
mysql_query($query);

mysql_close();
?>

 

On my actual script, I've got my username/pw/etc filled in, so that's not it... am I missing something else really simple? All this yields is a blank page. When I look at the employees table w/ MySQL Query Browser, I see that no entries are added.

 

Here's my HTML form...

<form action="insert.php" method="post">
First Name: <input type="text" name="firstName"><br>
Last Name: <input type="text" name="lastName"><br>
<input type="Submit">
</form>

 

On the same site, I setup a Phorum (PHP/MySQL based) message board that works with my database, so I'm sure that my server is setup properly.

 

I know this is ridiculously basic, but it's been driving me crazy for at least a week now and, again, help is extremely appreciated.  If you live in Minnesota, I'll buy you a beer.

<?php
$username="root";
$password="";
$database="test";

$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO employee VALUES ('$firstName','$lastName')";
mysql_query($query);

mysql_close();

?>

<form action="formtest.php" method="post">
First Name: <input type="text" name="firstName"><br>
Last Name: <input type="text" name="lastName"><br>
<input type="Submit">
</form>

 

That works for me. 

 

DB name of test, database called employee, and the 2 fields as firstname and lastname.

 

 

In response to the suggestions above... I ran this:

 

<?php
$username="xxx";
$password="xxx";
$database="xxx";

$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO employees (firstName,lastName)
VALUES
('$firstName','$lastName')";
mysql_query($query) or die (mysql_error());

mysql_close();
?>

 

And - again - got a blank page and nothing new in the DB.  No errors or anything.  This is probably significant, but I just haven't got a clue what it means and am double-confused since my message board works.  Any idea what to troubleshoot next?

 

Thanks again guys.

Is the table name employees?

Are the fieldnames firstName and lastName?

Are the fieldnames set to VARCHAR?

 

Remove the @ before mysql_select_db

Verify the DB name as well as the username, pw

 

And to restate, even if it works you'll get a blank screen because there is nothing after the query that will give you any output.

I got rid of the @ sign and double-checked my username/PW/database name.  Those were all correct, and the script still didn't update the employees table. 

 

The account I'm using has permission to Select, Delete, Update, Insert, Create and Drop stuff in the DB.  PHP and the MySQL are running on a machine here in my office (I'm working on a company intranet site).  Again, not sure if it's relevant, but maybe it makes a difference.  And thanks again.

Try something like this:

 

$bool = mysql_query($query);

if ($bool == TRUE)
    echo "The query was executed successfully.";
else
    echo "There was an error with the MySQL query.";

 

That way we at least have some output to help us see if the query is being executed properly.

Do you have error reporting turned off?  If you ran that script, you should have an output no matter what, since both the if and else statements output text.  If you have error reporting turned off then perhaps your script is encountering a fatal error and not executing fully, and you aren't being told of it.  Check your error logs or set error reporting to on.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.