Inserting help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Inserting help

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.';
  }    
}
?>
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

ok that quit making blanks but now it doesnt show anything in the database :?
User avatar
artexercise
Forum Commoner
Posts: 33
Joined: Thu Nov 20, 2003 9:38 am
Location: Raleigh, NC

Post by artexercise »

Do you have the mysql_connect statement in another file?

JOE--
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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());
Post Reply