PHP/MySQL Not Inserting to Database

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2178 Views - Last Post: 25 February 2013 - 09:17 AM Rate Topic: -----

#1 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 02:02 AM

Alright DIC, I am having a problem with my php file not accepting the input from my html form. I have tried changing it around every way I can, and I can't seem to get it to work.

I know the problem, I just scoured google and w3schools and other sites and they all have done the exact same thing I have, and for some reason mine won't accept the data. Any help is much appreciated.

Currently it adds an entry to my database, but the values are blank. And I am in total understanding that my variables are not getting any value put into them. What I don't understand is why? Every where I looked, I even copied and pasted code from other sites to try it, and it still didn't work. The one I have below is virtually an exact copy from w3schools and it still won't work. Any insight?

<html>
<body>

<form action="reg.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html> 

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];

$con = mysql_connect("127.0.0.1","root","");

mysql_select_db("my_db");

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$firstname','$lastname','$age')";
$result = mysql_query($sql);
if ($result)
  {
echo "1 record added";
  }
else{
	echo "Did not work";
}



mysql_close($con);
?> 


I am starting to wonder if it is an issue with my database.

As when the record adds, I still don't get the echo or print statements.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP/MySQL Not Inserting to Database

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 02:09 AM

as a start, echo out everything that has to do with your data, the variables itself and of course the query. also check the data types that your DB expects (i.e. I’d expect an age field to be an INT, not a (VAR)CHAR).

second, the usual disclaimer: the mysql extension is outdated and deprecated, use the modern PDO or MySQLi extensions instead.

PS. you are vulnerable to SQL injection. Prepared Statements help prevent that, but the outdated mysql extension does not provide any means to do that (while the newer extensions do).
Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 02:20 AM

Or one more thing you can do is to go to your phpAdmin page and check if the table has been created on not.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 02:54 AM

I am not worried about SQL Injections as this is just for learning at the moment. But I will definitely take that into consideration. I did echo out various elements and they worked when I ran the php file on its own, but not when I click sumbit on the html form.

When I click submit, it loads the php file, but I am assuming it does nothing after that as I have no entry in the database, and no echo statements.

Thanks for catching that one bug, I forgot to change it to INT.

Also, I will try the updated mysqli and see how that goes as well and get back to you.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 03:11 AM

since you have no echo statements, I can safely assume that you have a fatal error somewhere (otherwise there would be one of the two echo statements). enable error reporting and error display to see what it is.
Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 04:04 AM

View Postbteeple, on 25 February 2013 - 04:54 AM, said:

I am not worried about SQL Injections as this is just for learning at the moment.


This line of reason is just flat-out moronic. "I don't care that I'm learning to do it wrong, I'm just learning!" You should want -- FROM THE START -- to learn to do things PROPERLY. It's far more difficult to disabuse yourself of bad habits once learned.
Was This Post Helpful? 1
  • +
  • -

#7 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 04:31 AM

Who said I was learning it the wrong way?

I am learning it the way I know how. I am breaking down simple tasks and learning as I go. I start out with this simple form for inserting and retrieving data, and then I add on to it. That is the entire point to my learning experience. I know nothing of how to protect myself from SQL Injection. So I am learning the basics. Baby steps, if you will.

As I build on it, I will change code and improve it. Just like everything else and everyone else. Improving it as I go.
Was This Post Helpful? 0
  • +
  • -

#8 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 04:39 AM

But don't worry, that is next on my list once I get this to function correctly.
Was This Post Helpful? 0
  • +
  • -

#9 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 04:50 AM

<?php


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];

$con = mysqli_connect("127.0.0.1","root","");

mysqli_select_db($con, "my_db");

$sql="INSERT INTO persons (FirstName, LastName, Age)
VALUES
('$firstname','$lastname','$age')";
$result = mysqli_query($con, $sql);
if ($result)
  {
echo "1 record added";
  }
else{
	echo "Did not work";
}


error_reporting(E_ALL);
mysqli_close($con);
?> 


I changed things around a little bit and I fixed the problems with the error reporting except I am still getting undefined index on my $firstname, $lastname, and $age variables.

I should add, it is putting an entry in my database, but it is obviously empty. I don't really understand what is wrong with my variables, as to why they are not pulling the data from my fields in the form.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 05:01 AM

View Postbteeple, on 25 February 2013 - 12:50 PM, said:

I should add, it is putting an entry in my database, but it is obviously empty. I don't really understand what is wrong with my variables, as to why they are not pulling the data from my fields in the form.

then either you’re not POSTing or the names are different. you ned to test, what’s in the $_POST array.

PS. there’s no need for mysqli_select_db(), you can pass the DB name in the connector.
Was This Post Helpful? 0
  • +
  • -

#11 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 05:05 AM

There is nothing in the $_POST arrays, I just got done testing them.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,140
  • Joined: 08-June 10

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 05:09 AM

then you’re obviously not posting the form. cross-check with $_SERVER['REQUEST_METHOD'].
Was This Post Helpful? 0
  • +
  • -

#13 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 05:25 AM

After using the $_SERVER['REQUEST_METHOD'] command, I have found out I am using GET when I have them all set as POST. So I converted them all to GET and I kept troubleshooting, and I still run into the error of having nothing in the arrays and nothing echoing.

This is the sample code I have typed up to see if there is anything:

	echo $_SERVER['REQUEST_METHOD'];
	echo $_GET['firstname'];
    echo $_GET['lastname'];
	echo $_GET['age'];
?>



When I run that, I don't achieve anything. Just a blank screen, even the request method doesn't show up. The only way it shows up is if I run the php file on its own, outside of the html file. When I click submit on the form it does nothing. I think my problem lies there.
Was This Post Helpful? 0
  • +
  • -

#14 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 05:43 AM

<?php
echo $_SERVER['REQUEST_METHOD'];

if(isset($_GET['firstname'])){
	$firstname = $_GET['firstname'];
}
else{
	$firstname = 'null';
}
if(isset($_GET['lastname'])){
	$lastname = $_GET['lastname'];
}
else{
	$lastname = 'null';
}
if(isset($_GET['age'])){
	$age = $_GET['age'];
}
else{
	$age = 'null';
}

$con = mysqli_connect("127.0.0.1","root","", "my_db");


$sql="INSERT INTO persons (FirstName, LastName, Age)
VALUES
('$firstname','$lastname','$age')";
$result = mysqli_query($con, $sql);
if ($result)
  {
echo "1 record added";
  }
else{
	echo "Did not work";
}


error_reporting(E_ALL);
mysqli_close($con);
?> 



I updated my code for my php file to test if there is actually something in the post array. As of course, they are still not taking my data from my form inputs.

When I run the php file, it works. It just fills everything in as "null" in the database.

When I open the html file to use the form. I enter data into the fields and click submit, and only a blank screen pops up. I check the database and an entry was not entered.
Was This Post Helpful? 0
  • +
  • -

#15 bteeple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 01-May 11

Re: PHP/MySQL Not Inserting to Database

Posted 25 February 2013 - 06:00 AM

The form is not passing the variables, and I am lost as to what it needs to do. I even tried a simple piece of code like this:
(CODE IS FROM W3 SCHOOLS WEBSITE)
<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html> 

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html> 


The php file and form work together. But the variables are not being passed. I even changed it from POST to GET, and they are still not getting passed. I tested it with a few different small bits of code, and came to the conclusion on that.

Any idea on why the variables are not getting passed through or how I can go about fixing it? Is it a bad PHP install? I don't know why working code won't work when I haven't modified it or anything.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2