Jump to content

Connected - Input Does Not Populate Table


The_Thorn
Go to solution Solved by The_Thorn,

Recommended Posts

Hello.

 

I have been working at this for quite awhile now.

 

It is a rebuild of a successful DB from years ago (But I no longer have access to that code which is on a disabled machine).

 

Simply put, I am able to connect (No errors thrown), but no matter what I try I cannot post simple text to the table. I'm not sure why.

<html>
<body>

<form action="simpleTable3.html" method="post">
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
 
<input type="submit" />
</form>
 
<?php
$con = mysql_connect("sql2**.******.***","b8_14160309_simpleDB3","b8_14160309","c*******");
if (!$con)
  {
  die('Could not connect:' . mysql_error());
  }
 
mysql_select_db("b8_14160309_simpleDB3", $con);
 
$sql="INSERT INTO simpleTable3 (fname, lname)
VALUES
('$_POST[fname]','$_POST[lname]')";
 
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
 

?>
</body>
</html>

So, I need some assistance. Please. A hint or pointer in the right direction.

 

Thank you in advance!

 

The_Thorn

 

Link to comment
Share on other sites

Off hand I would say you are posting to an .html file...

Is your .htaccess allowing you parse php in an html file?

Should have an add type line in there

 

Thank you for your reply.

 

I am not sure how to answer the question about .htaccess; But, this does exist on the server by default.

 

I have tried the code first as .php - Did not work. So, I converted it to .html - Same problem. I'm not sure which file format is correct at this point.

Link to comment
Share on other sites

the file extension should be .php (anything else requires that you take a specific action to configure your server to work with php.)

 

when the file name ends in a .php, and the form action=' ... ' attribute is changed to match, what did occur in front of you when you submitted the form? did you get the "1 record added" message or what?

 

what URL are you using in your browser to request the page? if this is on a local development system, it should be something like http://localhost/your_file_name.php

 

lastly, the 4th parameter of the mysql_connect() statement is not something you usually supply. it is a flag that forces a new connection.

Link to comment
Share on other sites

the file extension should be .php (anything else requires that you take a specific action to configure your server to work with php.)

 

when the file name ends in a .php, and the form action=' ... ' attribute is changed to match, what did occur in front of you when you submitted the form? did you get the "1 record added" message or what?

 

what URL are you using in your browser to request the page? if this is on a local development system, it should be something like http://localhost/your_file_name.php

 

lastly, the 4th parameter of the mysql_connect() statement is not something you usually supply. it is a flag that forces a new connection.

 

Hi, and thanks for your help.

 

I will try to explain:

 

When the form is submitted, the fields are empty upon page refresh.

 

There are NO confirmations or errors displayed.

 

The URL I am using is: http://www.nemisis_iv.byethost8.com/simpleTable3.html (Please try it if you wish)

 

I am bit confused at this point. When I built this last year, before, it performed great. (This is just a test, a skeleton. When this works I can scale up. I just cannot get this to work)

 

Regards,

The_Thorn

Link to comment
Share on other sites

You're missing single quotes in in query on the $_POST array keys.

('$_POST[fname]','$_POST[lname]')
('$_POST['fname']','$_POST['lname']')

Assign the post variables to another set of variables (say $fname and $lname) and drop those into the query.

<?php
$con = mysql_connect("sql2**.******.***","b8_14160309_simpleDB3","b8_14160309","c*******");
if (!$con)
  {
  die('Could not connect:' . mysql_error());
  }

mysql_select_db("b8_14160309_simpleDB3", $con);

​$fname=$_POST['fname'];
$lname=$_POST['lname'];

$sql="INSERT INTO simpleTable3 (fname, lname)
VALUES
('$fname','$lname')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
?>

Also, part of the problem may be that you (and I, so not sure if mine will work either) are using variables inside single quotes in the query.

Edited by AJinNYC
Link to comment
Share on other sites

You're missing single quotes in in query on the $_POST array keys.

('$_POST[fname]','$_POST[lname]')
('$_POST['fname']','$_POST['lname']')

Assign the post variables to another set of variables (say $fname and $lname) and drop those into the query.

<?php
$con = mysql_connect("sql2**.******.***","b8_14160309_simpleDB3","b8_14160309","c*******");
if (!$con)
  {
  die('Could not connect:' . mysql_error());
  }

mysql_select_db("b8_14160309_simpleDB3", $con);

​$fname=$_POST['fname'];
$lname=$_POST['lname'];

$sql="INSERT INTO simpleTable3 (fname, lname)
VALUES
('$fname','$lname')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
?>

Also, part of the problem may be that you (and I, so not sure if mine will work either) are using variables inside single quotes in the query.

 

Thank you, AJinNYC!

 

I shall try this and report back.

 

(By the way - NYC? Great. I am QUITE close by).

 

Again, thank you and all others for your the help. It means a lot!

 

I will get this to work. (Eventually. LOL)

 

The_Thorn

Link to comment
Share on other sites

You're missing single quotes in in query on the $_POST array keys.

 

 

that's not 100% correct. as it is, that's valid syntax. adding the single-quotes will produce a php syntax error. if you need to put an associative array variable into a string, it's best to tell php where it starts and end by putting {} around it -

('{$_POST['fname']}','{$_POST['lname']}')

when i took a look at your .php page (the url you posted with the .html is 404), there's a mysql_connect error message (perhaps it is hidden by the iframe popup your host is outputting.) unfortunately, this error message is exposing a bunch of information about your account/database username. you should be learning php on a localhost development system and only put your code onto a live server after it is completely working and with no php settings or code that exposes any of your account information when an error occurs (hackers intentionally trigger errors to get this type of info.)

Link to comment
Share on other sites

that's not 100% correct. as it is, that's valid syntax. adding the single-quotes will produce a php syntax error. if you need to put an associative array variable into a string, it's best to tell php where it starts and end by putting {} around it -

('{$_POST['fname']}','{$_POST['lname']}')

when i took a look at your .php page (the url you posted with the .html is 404), there's a mysql_connect error message (perhaps it is hidden by the iframe popup your host is outputting.) unfortunately, this error message is exposing a bunch of information about your account/database username. you should be learning php on a localhost development system and only put your code onto a live server after it is completely working and with no php settings or code that exposes any of your account information when an error occurs (hackers intentionally trigger errors to get this type of info.)

 

Yes, still having issues. Now cannot connect to the server/DB (?) - The code has not changed regarding credentials.

 

But more importantly, mac_gyver, I just enrolled in an online class for PHP/MySql.

 

I studied "Database Design" in school, but it was highly theoretical, overall.

 

I am starting at the base-level to learn how to do this properly. The project I have planned is large and is worth the investment of my time to become a better programmer.

 

The_Thorn

Link to comment
Share on other sites

The code has not changed regarding credentials.

 

 

one of the problems with trying to develop code using a live server is you must make sure that the code you wrote actually made it onto the live server and is the code that is actually running and producing the result/symptom you are observing.

 

a forum like this sees a fair number of posts like - 'my code is doing something different today/now, then it did yesterday/the last time I tried it'. we also regularly see posted code that cannot possibly be producing the errors/symptoms that are being reported. this is often due to the code in your programming editor being different from the code that is actually running on the server, because of (pick one) - a) the code wasn't saved at all or to where you intended or there are multiple versions in different locations..., b) you or your editor didn't upload the code to the server or in the correct location on the server, c) the upload of the code failed and the error message to that effect was not noticed.

 

by using a localhost development system, the files are edited in-place and as long as you save your file when you close your editor (and you don't have a mess with same name files scattered throughout multiple folders in your project) you will know that the code that is running is the code you last saw in your programming editor.

Link to comment
Share on other sites

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.