Jump to content

Beginner PHP+MySQL help.


bundyxc

Recommended Posts

Yeah, I'm a total MySQL newbie, but am trying to run a little PHP/MySQL program on my site. Here's what I have so far.

 

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$grade = $_POST['grade'];
$age = $_POST['age'];
$11 = $_POST['11'];
$12 = $_POST['12'];
$13 = $_POST['13'];
$14 = $_POST['14'];
$21 = $_POST['21'];
$22 = $_POST['22'];
$23 = $_POST['23'];
$24 = $_POST['24'];

$con = mysql_connect("mysql","bundyxc","*****");

	if (!$con)
		{
			die('Error connecting to MySQL database: ' . mysql_error());
		}
mysql_select_db("ccf2", $con);
$sql = "INSERT INTO 'ccf2' ('firstname' ,'lastname' ,'grade' ,'age' ,'11' ,'12' ,'13' ,'14' ,'21' ,'22' ,'23' ,'24' ) VALUES ('$firstname','$lastname','$grade','$age','$11','$12','$13','$14','$21','$22','$23','$24');";
mysql_query($sql,$con);
mysql_close($con);
?>

 

I know that telling it to run off of 'mysql' is strange (and that the norm is localhost), but I know that I'm supposed to use 'mysql' as opposed to localhost. I'm running a forum system off of it, and I know for a fact that it works.

 

My database name is 'ccf2', and my table name is 'ccf2' as well. I'm sure that I'm just making some stupid syntax mistake, but I'm not running into any problems. I also can not get my script to post any errors (even when I do something like change the password to something incorrect).

 

I have this set up at bundyxc.com/cff2 if anybody wants to take a look at it and point me in the right direction. I've even tried to change the MySQL command so that it doesn't use any of the posted variables. I'm not even getting a null entry in my database.. it just isn't entering anything. Can somebody please help me? Thank you.

Link to comment
Share on other sites

Enable error messages

 

put this

error_reporting(E_ALL);
ini_set('display_errors','1');

 

at the beginning of your script.

 

Also change

mysql_query($sql,$con);

 

to

 

mysql_query($sql,$con) or die(mysql_error().": ".$sql);

 

to show any MySQL errors encoutered when executing query

 

[added]

 

And remove those quotes revraz posted about. You should use `` not '' around database objects' names (or you can refrain from any quotes all together around them)

Link to comment
Share on other sites

Thanks guys, I can't get it to work though. Should it look like this?

 

<?php

error_reporting(E_ALL);
ini_set('display_errors','1');

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$grade = $_POST['grade'];
$age = $_POST['age'];
$11 = $_POST['11'];
$12 = $_POST['12'];
$13 = $_POST['13'];
$14 = $_POST['14'];
$21 = $_POST['21'];
$22 = $_POST['22'];
$23 = $_POST['23'];
$24 = $_POST['24'];

$con = mysql_connect("mysql","bundyxc","*****");

	if (!$con)
		{
			die('Error connecting to MySQL database: ' . mysql_error());
		}
mysql_select_db("ccf2", $con);
$sql = "INSERT INTO ccf2 (firstname, lastname, grade, age, 11, 12, 13, 14, 21, 22, 23, 24) VALUES ('$firstname','$lastname','$grade','$age','$11','$12','$13','$14','$21','$22','$23','$24');";
mysql_query($sql,$con) or die(mysql_error().": ".$sql);
mysql_close($con);
?>

Link to comment
Share on other sites

Which means it will only echo if there is an error.  He should echo it to check his variables for troubleshooting.

 

It's already there, next to mysql_error()

If query encountered any problems during execution it should be displayed along with error message.

Link to comment
Share on other sites

He said "he can't get it to work", do you have a mind reading device that explains exactly what that means?  It can insert a row with blank data if the fields allow that.  It doesn't have to throw a mysql_error in order to "not work".

 

You should learn to not assume.

Link to comment
Share on other sites

Even if I'm not correctly posting my variables, it should be inserting a null row, or at least that's my understanding of it. But I have no blank rows. Absolutely nothing.

 

I went into my phpmyadmin, and manually inserted a row (to make sure that my database wasn't doing anything funky) and it worked perfectly.

 

Right now my code says this:

   $con = mysql_connect("mysql","bundyxc","*****");
   
      if (!$con)
         {
            die('Error connecting to MySQL database: ' . mysql_error());
         }
   mysql_select_db("ccf2", $con);

 

Shouldn't it look like this?

 

   $con = mysql_connect("mysql","bundyxc","*****");
   
   mysql_select_db("ccf2", $con);

      if (!$con)
         {
            die('Error connecting to MySQL database: ' . mysql_error());
         }

 

EDIT: I tried changing it.. still no luck.  :(

Link to comment
Share on other sites

It shouldn't make a difference, and it is more logical as it is now.

 

You have

 

mysql_connect

 

Which tries to estabilish a connection to MySQL server, and if successful, returns a connection handle to $con

 

Then you check if this handle exists, and if not, display an error message.

 

Then you select a database to work with.

 

You could change

mysql_select_db("ccf2", $con);

 

$db_selected = mysql_select_db("ccf2", $con);
if (!$db_selected) {
    die ('Can\'t use ccf2 : ' . mysql_error());
}

 

to display error message, if database cannot be selected for any reasons.

Link to comment
Share on other sites

Your field names and mysql column names are improper.  The first error I received is that $11 is an unexpected T_LNUMBER.

 

Make your <input /> field names more meaningful or at least start with a letter.  Meaning, make $11 -> $14 & $21 -> $24 variables prefixed like $p11 , $p12, $p13 .. etc.  That solves the first part.

 

 

The second part is that your MySQL column names are improperly named: regarding the 11-14, 21-24 thing.  You had the right idea using quotes before... but you should rename your column names, or at least have them start with a letter.  If you wish to keep the column names, enclose the fields in ' or backticks `.  But once again, better to make them more meaningfully named for better portability.

 

 

Fix those syntax and proper naming convention errors and it should work just fine.  I don't know how you missed the errors.. it popped up right away on mine.

 

 

My debug code for reference:

<?php

   error_reporting(E_ALL);
   ini_set('display_errors','1');

if(isset($_POST['student'])){
   $firstname = $_POST['firstname'];
   $lastname = $_POST['lastname'];
   $grade = $_POST['grade'];
   $age = $_POST['age'];
   $p11 = $_POST['11'];
   $p12 = $_POST['12'];
   $p13 = $_POST['13'];
   $p14 = $_POST['14'];
   $p21 = $_POST['21'];
   $p22 = $_POST['22'];
   $p23 = $_POST['23'];
   $p24 = $_POST['24'];

   $con = mysql_connect("*****","****","*****");
   
      if (!$con)
         {
            die('Error connecting to MySQL database: ' . mysql_error());
         }
   mysql_select_db("******", $con);
   $sql = "INSERT INTO ccf2 (firstname, lastname, grade, age, `11`, `12`, `13`, `14`, `21`, `22`, `23`, `24`) VALUES ('$firstname','$lastname','$grade','$age','$p11','$p12','$p13','$p14','$p21','$p22','$p23','$p24');";
   mysql_query($sql,$con) or die(mysql_error().": ".$sql);
   mysql_close($con);
}else{
?>

<form method="post">
  Fn<input type="text" name="firstname" /><br >
  Ln<input type="text" name="lastname" /><br >
  Grade<input type="text" name="grade" /><br >
  Age<input type="text" name="age" /><br >
  11<input type="text" name="11" /><br >
  12<input type="text" name="12" /><br >
  13<input type="text" name="13" /><br >
  14<input type="text" name="14" /><br >
  21<input type="text" name="21" /><br >
  22<input type="text" name="22" /><br >
  23<input type="text" name="23" /><br >
  24<input type="text" name="24" /><br >
  
<input type="submit" name="student" value="Go" />
</form>

<?php } ?>

 

edit: almost left my database credentials lol

Link to comment
Share on other sites

Wow. I feel like a total idiot.

 

haha, thank you guys for your help.. but I'm still having problems. It isn't posting any errors, but the script will echo when I add a little:

 

echo "Test test test.";

 

Before it wasn't doing that. Nor would it show any HTML that I put into script.php. So now it looks like the script should be running.. but it isn't inserting anything into the table.  ???

 

 

<?php

error_reporting(E_ALL);
ini_set('display_errors','1');

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$grade = $_POST['grade'];
$age = $_POST['age'];
$p11 = $_POST['p11'];
$p12 = $_POST['p12'];
$p13 = $_POST['p13'];
$p14 = $_POST['p14'];
$p21 = $_POST['p21'];
$p22 = $_POST['p22'];
$p23 = $_POST['p23'];
$p24 = $_POST['p24'];

$con = mysql_connect("mysql","********","********");

if (!$con) {
	die('Error connecting to MySQL database: ' . mysql_error());
	}

mysql_select_db("ccf2") or die(mysql_error());

$db_selected = mysql_select_db("ccf2", $con);

if (!$db_selected) {
	 die ('Can\'t use ccf2 : ' . mysql_error());
	}

$query = "INSERT INTO ccf2 (firstname, lastname, grade, age, '11', '12', '13', '14', '21', '22', '23', '24') VALUES ('$firstname','$lastname','$grade','$age','$p11','$p12','$p13','$p14','$p21','$p22','$p23','$p24');"; 
mysql_close($con);

?>

This is HTML.

Link to comment
Share on other sites

>:( It won't let me double-edit my post. So I guess I'll have to resort to double-posting. SORRY!

 

After looking at my code, I realized that the reason my last code didn't work was becuase of  me forgetting to put this in..

 

mysql_query($sql,$con) or die(mysql_error().": ".$sql);

 

Thank you guys so much for your help.  :D

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.