Page 1 of 1

Inserting help

Posted: Mon Jun 06, 2005 11:13 am
by Smackie
For some reason this script will only send blanks to MySQL why is it sending blanks??

Code: Select all

<?php

if(isset($_POST['post'])) {

$site_name = $_POST['site_name'];
$tablecolor = $_POST['tablecolor'];
$background = $_POST['background'];

}

$sql = "INSERT INTO sitestuff (site_name, tablecolor, background) VALUES ('$site_name', '$tablecolor', '$background')";


echo '<form name="form" method="post" action="../install.php?pages=site-stuff">';
echo 'Site Name:<input name="$site_name" type="text" size="35" maxlength="100"><br>';
echo 'Table Color:<input name="$tablecolor" type="text" size="35" maxlength="100"> <font color="red">*Please add # before the numbers or just use red, blue ect.*</font><br>';
echo 'Background Color:<input name="$background" type="text" size="35" maxlength="100"> <font color="red">*Please add # before the numbers or just use red, blue ect.*</font><br>';
echo '<input type="submit" name="Submit" value="Submit"></form>';

$result = mysql_query($sql);

if (!$result) {

	echo '<b>Error!</b> Please contact the administrator.';
	
} else {

	echo 'Tables were successfully added.';
	
}

?>
thank you
Smackie

Posted: Mon Jun 06, 2005 11:22 am
by Skara
you're sending the query even if the form hasn't been submitted.

Code: Select all

<?php
echo '<form name="form" method="post" action="../install.php?pages=site-stuff">';
echo 'Site Name:<input name="$site_name" type="text" size="35" maxlength="100"><br>';
echo 'Table Color:<input name="$tablecolor" type="text" size="35" maxlength="100"> <font color="red">*Please add # before the numbers or just use red, blue ect.*</font><br>';
echo 'Background Color:<input name="$background" type="text" size="35" maxlength="100"> <font color="red">*Please add # before the numbers or just use red, blue ect.*</font><br>';
echo '<input type="submit" name="Submit" value="Submit"></form>';

if(isset($_POST['post'])) {
  $site_name = $_POST['site_name'];
  $tablecolor = $_POST['tablecolor'];
  $background = $_POST['background'];
  $sql = "INSERT INTO sitestuff (site_name, tablecolor, background) VALUES ('$site_name', '$tablecolor', '$background')";
  $result = mysql_query($sql);
  if (!$result) {
    echo '<b>Error!</b> Please contact the administrator.';
  } else {
    echo 'Tables were successfully added.';
  }    
}
?>

Posted: Mon Jun 06, 2005 11:30 am
by Smackie
ok that quit making blanks but now it doesnt show anything in the database :?

Posted: Mon Jun 06, 2005 11:34 am
by artexercise
Do you have the mysql_connect statement in another file?

JOE--

Posted: Mon Jun 06, 2005 11:35 am
by Skara
..I didn't look at your form. -_-'

Code: Select all

&lt;input name=&quote;$site_name&quote; type=&quote;text&quote; size=&quote;35&quote; maxlength=&quote;100&quote;&gt;
should be

Code: Select all

&lt;input name=&quote;site_name&quote; type=&quote;text&quote; size=&quote;35&quote; maxlength=&quote;100&quote;&gt;
and you never once set $_POST['post'] in the form. O.o

Change

Code: Select all

if(isset($_POST['post'])) {
to

Code: Select all

if(isset($_POST['site_name'])) {
If this is a public form, I advise against directly entering the data. Convert quotes etc into html entities or something.

Edit: ^ posted.
Do you have the mysql_connect statement in another file?
If he didn't, he wouldn't have gotten blanks. :P

Posted: Mon Jun 06, 2005 11:38 am
by Smackie
If you mean db.php that connects to the database yeah

Code: Select all

<? 
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = 'localhost';
$dbuser = '';
$dbpasswd = '******';
$database = '';

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbuser","$dbpasswd") 
	or die ("Couldn't connect to server.");
	
$db = mysql_select_db("$database", $connection)
	or die("Couldn't select database.");
?>
it is stored in install.php file

Posted: Mon Jun 06, 2005 12:00 pm
by John Cartwright
on every mysql_*() you should run a or die(mysql_error()) to spot the problem. For example,

Code: Select all

$result = mysql_query($sql);
to

Code: Select all

$result = mysql_query($sql) or die(mysql_error());