Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

PHP code input data to db

Hello Experts,

I think I am close, but not quite sure what I am doing wrong...

I am trying to send data from an HTML form into a mysql database using PHP. I have a file called Insert.php that I got from a tutorial, but I am missing something. When I try to run it, I don't get any errors, but when I go to the database, the data does not get there.

I have made sure all my names are the same between the db and php code. what else could be the problem?

I think I am not using the correct code to connect to the database. Here is the PHP file...

<?
$username="ClassPTA";
$password="classPTA012";
$database="Volunteers";

$first=$_POST['First'];
$last=$_POST['Last'];
$phone=$_POST['Phone'];
$EMail=$_POST['EMail'];
$Child=$_POST['Child'];
$Grade=$_POST['Grade'];
$Teacher=$_POST['Teacher'];
$RoomParent=$_POST['RoomParent'];
$Classroom=$_POST['Classroom'];
$Projects=$_POST['Projects'];
$TrackandField=$_POST['TrackandField'];
$Other=$_POST['Other'];


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

$query = "INSERT INTO vol' ($First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";
mysql_query($query);

mysql_close();
?>
0
Jennerator
Asked:
Jennerator
  • 11
  • 8
  • 4
  • +3
4 Solutions
 
jasonsbytesCommented:
Try:

                $db_name     = 'volunteers';
                $db_username = 'ClassPTA';
                $db_password = 'classPTA012';
                mysql_connect('localhost', $db_username, $db_password);

$query = "INSERT INTO vol VALUES ($First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";

mysql_db_query($db_name, $query);


0
 
Kent OlsenData Warehouse Architect / DBACommented:

This is going to sound silly, but php block generally start with

  <?php

The tag <? simply starts a comment.


Kent
0
 
jasonsbytesCommented:
Kdo has a point... Depending on hour your php is configured, you can somtimes use just <? but it's better to use <?php.  That may be your solution.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
JenneratorAuthor Commented:
This is my first attempt at PHP so nothing is silly at this point. I'll try it out and let you know.
0
 
JenneratorAuthor Commented:
OK, Here is what the code looks like now..

<?php
$db_name     = 'volunteers';
$db_username = 'pattonpta';
$db_password = 'patton6001';

mysql_connect('localhost', $db_username, $db_password);

$first=$_POST['First'];
$last=$_POST['Last'];
$phone=$_POST['Phone'];
$EMail=$_POST['EMail'];
$Child=$_POST['Child'];
$Grade=$_POST['Grade'];
$Teacher=$_POST['Teacher'];
$RoomParent=$_POST['RoomParent'];
$Classroom=$_POST['Classroom'];
$Projects=$_POST['Projects'];
$TrackandField=$_POST['TrackandField'];
$Other=$_POST['Other'];

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

$query = "INSERT INTO vol VALUES ($First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";

mysql_db_query($db_name, $query);

mysql_close();
?>

This is the error I got...

Warning: mysql_connect(): Access denied for user: 'apache@localhost' (Using password: NO) in /var/www/vhosts/pattonpta.org/httpdocs/Insert.php on line 23
Unable to select database
0
 
JenneratorAuthor Commented:
This is line 23

mysql_connect(localhost,$username,$password);

Any ideas?
0
 
DrNikon224Commented:
You define the db username and password:

$db_username = 'pattonpta';
$db_password = 'patton6001';

but don't use the right variable name in your connection string:

mysql_connect(localhost,$username,$password);

Use:

mysql_connect(localhost,$db_username,$db_password);
0
 
Kent OlsenData Warehouse Architect / DBACommented:
You've already done a mysql_connect () (about line 6).


0
 
DrNikon224Commented:
So he has - strike my comment.

Drop line 23 altogether, or fix it and drop the first connect statement.

Also, in your query, you're missing the initial single-quote before $First:

$query = "INSERT INTO vol VALUES ($First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";

instead:

$query = "INSERT INTO vol VALUES ('$First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";
0
 
JenneratorAuthor Commented:
So close...

Now i just get the error "unable to select database."

Here is what the code looks like now...
<?php
$db_name     = 'volunteers';
$db_username = 'pattonpta';
$db_password = 'patton6001';

$first=$_POST['First'];
$last=$_POST['Last'];
$phone=$_POST['Phone'];
$EMail=$_POST['EMail'];
$Child=$_POST['Child'];
$Grade=$_POST['Grade'];
$Teacher=$_POST['Teacher'];
$RoomParent=$_POST['RoomParent'];
$Classroom=$_POST['Classroom'];
$Projects=$_POST['Projects'];
$TrackandField=$_POST['TrackandField'];
$Other=$_POST['Other'];


mysql_connect(localhost,$db_username,$db_password);

@mysql_select_db($db_name) or die( "Unable to select database");

$query = "INSERT INTO vol VALUES

('$First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";

mysql_db_query($db_name, $query);


mysql_close();
?>
0
 
jasonsbytesCommented:
REMOVE this line:
@mysql_select_db($db_name) or die( "Unable to select database");

You don't need it.  You are specifying which db_name in the mysql_db_query.
0
 
AlexanderRCommented:
Well, actually he does.  How else mysql can know what DB he is working on.  The error that i see is that localhost is not written as string correctly.

mysql_connect(localhost,$db_username,$db_password);

must be
mysql_connect("localhost",$db_username,$db_password);
0
 
jasonsbytesCommented:
Alex, she does NOT need that line.

Mysql knows which db she is working with based on this line which specifies it:
mysql_db_query($db_name, $query);

and yes, she does need to quote "localhost" in the connect line.
0
 
JenneratorAuthor Commented:
Well, I tried it with the line and I got the error. Then I tried it without the line and I don't get the error, but when I check the database, the data is not there.

Here is how it is looking now.

<?php
$db_name     = 'volunteers';
$db_username = 'pattonpta';
$db_password = 'patton6001';

$first=$_POST['First'];
$last=$_POST['Last'];
$phone=$_POST['Phone'];
$EMail=$_POST['EMail'];
$Child=$_POST['Child'];
$Grade=$_POST['Grade'];
$Teacher=$_POST['Teacher'];
$RoomParent=$_POST['RoomParent'];
$Classroom=$_POST['Classroom'];
$Projects=$_POST['Projects'];
$TrackandField=$_POST['TrackandField'];
$Other=$_POST['Other'];


mysql_connect("localhost",$db_username,$db_password);



$query = "INSERT INTO vol VALUES

('$First','$Last','$Phone','$EMail','$Child','$Grade','$Teacher','$RoomParent','$Classroom','$Projects','$TrackandField','$Other')";

mysql_db_query($db_name, $query);


mysql_close();
?>
For some reason it is just not finding the database. is there something I should be doing outside of the code?

Thanks again for all your help!
0
 
jasonsbytesCommented:
add this line immediately after the mysql_db_query line:


echo "Error: " . mysql_errno().": ".mysql_error()."<BR>";

Let me know if it outputs anything.
0
 
JenneratorAuthor Commented:
Error: 1044: Access denied for user: 'pattonpta@localhost' to database 'volunteers'
0
 
jasonsbytesCommented:
ok, that means either your userid or password or database name or hostname is incorrect.
0
 
JenneratorAuthor Commented:
Well, you were right of course. the V's needed to be capitalized for Volunteer (database) and Vol (table).

Now I get the following error...

Error: 1136: Column count doesn't match value count at row 1
I am unsure where to go with this.

I can't tell you how much I appricate your help with this. I don't know if I mentioned it, but this is a website for an elementary school and all my time is donated. Of course I could have just gone with an e-mail submit, but I aim big!

Jenn~
0
 
AlexanderRCommented:
For that you need to look at your vol table structure in the database.  The order of the columns in the table needs to exactly match the order of your VALUES( ).
To look at your database table you can either use phpmyadmin or mysql gui tools
0
 
jasonsbytesCommented:
or you can specify the columns in the query just before values add:

(col1, col2, etc.)

add them in the same order as the values(data1, data2, etc.)
0
 
AymanAdamCommented:
hi,

suppose you have a table named users with this structure:

id           name             address
==========================

id is integer value, others are varchar

now to insert your data you either mention the columns with the table order:

$query= "insert into users values ($id, '$name', '$address')";

or specify the columns explicitly as:

$query= "insert into users (address,id,name) values ( '$address',$id, '$name',)";

and another thing, if the column is number value don't put it between single quotes '' ,
as the above example note that id is integer so in the insert statement i didn't put surrounding quotes in id where the other columns are varchar they need these quotes to be putted.


good luck,,,,
Ayman
0
 
JenneratorAuthor Commented:
You guys have been amazing. I am learning so much! I have lined up the fieldnames in the table, insert.php file and .html file and when I submit I get an Error: 0: .

When I go to the table, all the field are being inputted to except First, Last and Phone. I have checked and rechecked the field names and make sure they were the same accross the board. why won't they input?

Lastly, and I'll add extra points for this since it wasn't part of the original question. once the form is working and adding all the data into the database, how do I get a different page to display instead of a blank one? I have created a SubmitReq.html page that just says something to the effect that their data was received and then they can move on.

Oh I think we are almost there!!
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Jennerator,

(I just love that name.  I've got a niece named Jennifer that, with your permission, is about to acquire 'The Jennerator' as a nickname.)

Your PHP scripts writes to standard output.  This output is sent to the web server, which sends it to the browser.  The output is generally HTML.

Write HTML to stdout.  In fact, this is a good teaching point.  Start by echoing an HTML file header, then within the script echo the contents of each variable that you're storing (along with a label).  At the end of the script echo the HTML file trailer.


Kent
0
 
jasonsbytesCommented:
Just move the content of SubmitReq.html to your submit page and let php output that page after inserting the data to the db.
0
 
JenneratorAuthor Commented:
I was able to echo out the fields and they all come thru. Why won't First, Last and Phone store in the database? What am I missing there.

Kdo, feel free to use Jennerator on your neice, I am also a Jennifer and I got that nick name from an old Teacher/boss who taught me computers! I have loved it ever since.

Thanks,
Jenn~

0
 
Kent OlsenData Warehouse Architect / DBACommented:

Can you post the table definition?

(And my niece also goes by Jenn)   :)


Kent
0
 
JenneratorAuthor Commented:
Field Type Null Default
First   text No    
Last   text No    
Phone   varchar(30) No    
EMail   varchar(50) No    
Child   varchar(50) No    
Grade   varchar(5) No    
Teacher   varchar(50) No    
RoomParent   text No    
Carnival   text No    
Projects   text No    
Track&Field   text No    
Classroom   text No    
Other   text No    

I printed this out from the PHPmyAdmin database There is no reason that I have some TEXT and some VarChar, Other then I can' t seem to get it to change after the fact. LIke I said all the data goes into the right places except First, Last and Phone stay blank every time I submit.

Jenn~
0
 
AymanAdamCommented:
hi jenn,
are you sure that you have get First, Last and Phone from the user input form? be sure that you have get this data from the form .
e.g: $first=$_POST['first'];
and also check the variable names are they correct (spelling, letters, ....etc) as PHP variables are case sensitive $First is not same as $first. some times these things happen, right?!
Ayman
0
 
DrNikon224Commented:
$first, $last, and $phone are defined in lowercase, but used in title case in the query ($First, $Last, $Phone).
0
 
JenneratorAuthor Commented:
It is working!!! You have all been so helpful I don't even know where to start to spilt the points and unfortunatly I have so little to work with. Just know that the PTA, parents and kids at Patton Elementary send their deepest gratatude!

Thanks again,
Jenn~
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 8
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now