Page 1 of 3

Data not going into SQL

Posted: Tue Aug 02, 2005 4:40 am
by Devnull
I have got the following funcions:

Code: Select all

function filled_out($form_vars) {
	
	foreach ($form_vars as $keys => $value) {
		if (!isset($key) || ($value == ''))
			return false;
		}
	return true;
}

function insert($city, $area, $street, $unit, $unitl, $incident, $property, $andiamos, $bbgs, $telxos, $obervations, $ip) {
	
	$conn = db_connect();
	if (!$conn)
		require_once('header.html');
		return '<div id="error">No se ha podido establecer una conexi&oacute;n a la base de datos. Por favor, int&eacute;ntelo m&aacute;s tarde.</div>';
		require_once('footer.html');
		
		$result = mysql_query("INSERT INTO `informes` (`id`, `city`, `area`, `street`, `unit`, `unitl`, `incident`, `property`, `andiamos`, `bbgs`, `telxos`, `observations`, `ip`) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
		if (!result)
		require_once('header.html');
		return '<div id="error">No se ha podido agregar la informaci&oacture;n a la base de datos.</div>';
		require_once('footer.html');
		
		return true;
	}
And I got a form, this is the processing script:

Code: Select all

require_once('includes/global.php');

$city = $HTTP_POST_VARS['city'];
$street = $HTTP_POST_VARS['street'];
$unit = $HTTP_POST_VARS['unit'];
$unitl = $HTTP_POST_VARS['unitl'];
$incident = $HTTP_POST_VARS['incident'];
$property = $HTTP_POST_VARS['property'];
$andiamos = $HTTP_POST_VARS['andiamos'];
$bbgs = $HTTP_POST_VARS['bbgs'];
$telxos = $HTTP_POST_VARS['telxos'];
$observations = $HTTP_POST_VARS['observations'];
$ip = $REMOTE_ADDR;

$area = '1'; /* Change this value! */

if (!filled_out($HTTP_POST_VARS)) {
	require_once('header.html');
	echo '<div class="error">No ha rellenado el formulario completamente, aseg&uacute;rese de que este rellenado y despu&eacute;s pulse agregar.</div>';
	require_once('footer.html');
	exit;
}


$insert_result = insert($city, $area, $street, $unit, $unitl, $incident, $property, $andiamos, $bbgs, $telxos, $obervations, $ip);

 if ($insert_result = true) {
	require_once('header.html');
	echo '<div id="error">&iexcl;La informaci&oacute;n que ha enviado ha sido a&ntilde;adida a la base de datos con &eacute;xito!</div>';
	echo '$insert_result';
	require_once('footer.html');
	}

else {
		require_once('header.html');
		echo '<div id="error">No se ha podido guardar la informaci&oacute;n a la base de datos. Por favor, int&eacute;ntelo de nuevo.</div>';
		echo '$insert_result';
		require_once('footer.html');
		exit;
	}
I don't know why, but when the user hits submit, he/she gets returned to the same form page with all fields blank and nothing added to the databse, I just don't understand why. And yes, I have also user the HTTP_POST_VARS so the values can't be empty!

What have I done wrong ?

EDIT:

I have slightly modified my processing script:

Code: Select all

$insert_result = insert($city, $area, $street, $unit, $unitl, $incident, $property, $andiamos, $bbgs, $telxos, $obervations, $ip);

if ($insert_result === true) {
	require_once('header.html');
	echo '<div class="error">&iexcl;La informaci&oacute;n que ha enviado ha sido a&ntilde;adida a la base de datos con &eacute;xito!</div>';
	require_once('footer.html');
}

else {
		require_once('header.html');
		echo '<div class="error">No se ha podido guardar la informaci&oacute;n a la base de datos. Por favor, int&eacute;ntelo de nuevo.</div>';
		require_once('footer.html');
		exit;
	}
Now I get my error message saying that the information couldn't be saved ? How can I find out what's causing the problem ?

Posted: Tue Aug 02, 2005 4:45 am
by Skittlewidth

Code: Select all

$result = mysql_query("INSERT INTO `informes` (`id`, `city`, `area`, `street`, `unit`, `unitl`, `incident`, `property`, `andiamos`, `bbgs`, `telxos`, `observations`, `ip`) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
Try un-quoting the column names and table name eg:

Code: Select all

$result = mysql_query("INSERT INTO informes (id, city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");

Posted: Tue Aug 02, 2005 4:46 am
by shiznatix
try the standard mysql_query() or die(mysql_error());

really just put "or die(mysql_error())" after the mysql_query to see what error you are getting

Posted: Tue Aug 02, 2005 4:52 am
by s.dot
er I've done this a couple times, and I don't see your form action/method in your post, so it could be it

Check that your form action is going to the right page, and that the method is post.

Posted: Tue Aug 02, 2005 5:03 am
by Devnull
Skittlewidth wrote:

Code: Select all

$result = mysql_query("INSERT INTO `informes` (`id`, `city`, `area`, `street`, `unit`, `unitl`, `incident`, `property`, `andiamos`, `bbgs`, `telxos`, `observations`, `ip`) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
Try un-quoting the column names and table name eg:

Code: Select all

$result = mysql_query("INSERT INTO informes (id, city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
I tried doing that, and now I get the message saying that my details were successfully entered, but still they don't show up on the table.

Posted: Tue Aug 02, 2005 5:05 am
by Devnull
This is the structure of my table:

Code: Select all

CREATE TABLE `informes` (
  `id` tinyint(3) NOT NULL auto_increment,
  `city` tinytext NOT NULL,
  `area` tinytext NOT NULL,
  `street` mediumtext NOT NULL,
  `unit` tinytext NOT NULL,
  `unitl` tinytext NOT NULL,
  `incident` tinytext NOT NULL,
  `property` tinytext NOT NULL,
  `andiamos` tinytext NOT NULL,
  `bbgs` tinytext NOT NULL,
  `telxos` tinytext NOT NULL,
  `observations` tinytext NOT NULL,
  `ip` mediumtext NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
shiznatix wrote:try the standard mysql_query() or die(mysql_error());

really just put "or die(mysql_error())" after the mysql_query to see what error you are getting
I added this and I don't get any errors, I just get the message saying that the details were sucessfuly added into the database.
scrotaye wrote:er I've done this a couple times, and I don't see your form action/method in your post, so it could be it

Check that your form action is going to the right page, and that the method is post.
My form is going to the right page and my method is POST.

Posted: Tue Aug 02, 2005 5:33 am
by Skittlewidth

Code: Select all

$result = mysql_query("INSERT INTO informes (id, city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
You appear to be inserting a blank value into your auto_increment id field. Would that cause a problem? You should be leaving this to auto_increment on its own.

Have you checked that the post variables are even reaching this function by echoing them out first?

Posted: Tue Aug 02, 2005 5:36 am
by Devnull
Skittlewidth wrote:

Code: Select all

$result = mysql_query("INSERT INTO informes (id, city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('', '$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip');");
You appear to be inserting a blank value into your auto_increment id field. Would that cause a problem? You should be leaving this to auto_increment on its own.

Have you checked that the post variables are even reaching this function by echoing them out first?
Yes, I found that out, I added $result = mysql_query($sql) or die(mysql_error()); and it says my query was empty, what value do I put in the ID field when I am inserting the query ?

Posted: Tue Aug 02, 2005 5:38 am
by Skittlewidth

Code: Select all

$result = mysql_query("INSERT INTO informes ( city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip')");
Leave the id column out of the insert query altogether and it will increment on its own, hence the name.

Also you might want to remove the ";" at the end of your insert statement.

Posted: Tue Aug 02, 2005 5:41 am
by Devnull
The variables don't seem to be reaching the page, I have added the following code to check it and it only display $ip.

Code: Select all

<?php

echo "$city";
echo "$street";
echo "$unit";
echo "$unitl";
echo "$incident";
echo "$property";
echo "$andiamos";
echo "$bbgs";
echo "$telxos";
echo "$observations";
echo "$ip";

?>
What's wrong ?

Posted: Tue Aug 02, 2005 5:46 am
by Skittlewidth
Can you show us the html form code?

also Register Variables should be OFF in your PHP setup, so to call the variables should be:

Code: Select all

echo $_POST['city']; 
echo $_POST['street']; 
echo $_POST['unit']; 
echo $_POST['unitl']; 
echo $_POST['incident']; 
echo $_POST['property']; 
echo $_POST['andiamos']; 
echo $_POST['bbgs']; 
etc.....

Posted: Tue Aug 02, 2005 5:51 am
by Devnull
The variables seem to be working fine now, but it still says the Query was empty. I have setup an online version:

http://xirgo.net/portfolio/telereport

User: demo
Pass: demo

I have already used the HTTP_POST_VARS:

Code: Select all

$city = $HTTP_POST_VARS['city'];
$street = $HTTP_POST_VARS['street'];
$unit = $HTTP_POST_VARS['unit'];
$unitl = $HTTP_POST_VARS['unitl'];
$incident = $HTTP_POST_VARS['incident'];
$property = $HTTP_POST_VARS['property'];
$andiamos = $HTTP_POST_VARS['andiamos'];
$bbgs = $HTTP_POST_VARS['bbgs'];
$telxos = $HTTP_POST_VARS['telxos'];
$observations = $HTTP_POST_VARS['observations'];
$ip = $REMOTE_ADDR;
The HTML code to my form:
<div id="loginbox">
Rellene el formulario siguiente y a continuaci&oacute;n pulse agregar.
<form method="post" action="insert.php">
<div class="lalt">Cuidad:</div>
<div class="lalt"><input name="city" id="city" type="text" class="field" /></div>
<div class="lalt"> Direcci&oacute;n:</div>
<div class="lalt"><input name="street" type="text" class="field" /></div>
<div class="lalt">N&ordm; Soporte / Letra de Soporte :</div>
<div class="lalt"><input name="unit" type="text" class="field" size="10"/> / <input name="unitl" type="text" class="field" size="10"/></div>
<div class="lalt">Incidente:</div>
<div class="lalt"><input name="incident" type="text" class="field" /></div>
<div class="lalt">Propiedad:</div>
<div class="lalt"><select name="property"><option value="Si">Si</option><option value="No">No</option></select></div>
<div class="lalt">Pegatina de Andiamo :</div>
<div class="lalt"><select name="andiamos"><option value="Si">Si</option><option value="No">No</option></select></div>
<div class="lalt">Pegatina de BBG :</div>
<div class="lalt"><select name="bbgs"><option value="Si">Si</option><option value="No">No</option></select></div>
<div class="lalt">Pegatina de Telxo :</div>
<div class="lalt"><select name="telxos"><option value="Si">Si</option><option value="No">No</option></select></div>
<div class="lalt">Observaciones:</div>
<div class="lalt"><textarea name="observations" cols="65" rows="5" class="field"></textarea></div>
<div class="lalt"><input type="image" src="images/agregar.gif" value="Autentificar" /></div>
</form>
</div>

Posted: Tue Aug 02, 2005 6:13 am
by CoderGoblin
add an echo to your php

Always a good idea to place the connection into the sql command...

Code: Select all

$result = mysql_query($conn,"INSERT INTO informes (city, area, street, unit, unitl, incident, property, andiamos, bbgs, telxos, observations, ip) VALUES ('$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip')");
For debugging you can also add..

Code: Select all

echo("SQL = INSERT INTO informes (`city`, `area`, `street`, `unit`, `unitl`, `incident`, `property`, `andiamos`, `bbgs`, `telxos`, `observations`, `ip`) VALUES ('$city', '$area', '$street', '$unit', '$unitl', '$incident', '$property', '$andiamos', '$bbgs', '$telxos', '$observations ', '$ip')");
just before the execution and see what that returns. You could then try that using SQLAdmin or whatever you use to adminitsrate MySQL away from the php.

For future reference you may want to use var_dump($_POST); or print_r($_POST); rather than all your echos (Saves typing ) :wink:.

If the echo comes out empty I'll have another look.

Posted: Tue Aug 02, 2005 6:17 am
by Devnull
I get the following:

Code: Select all

SQL = INSERT INTO informes (`city`, `area`, `street`, `unit`, `unitl`, `incident`, `property`, `andiamos`, `bbgs`, `telxos`, `observations`, `ip`) VALUES ('001', '1', '002', '003', '004', '005', 'Si', 'Si', 'Si', 'Si', ' ', '83.53.199.156')
Looks like there's a problem on the observation field...

Posted: Tue Aug 02, 2005 6:21 am
by CoderGoblin
You may also need to check the database types for the table columns. If set to integer '001' may cause problems. Any integer types should not be quoted. I know I have had problems with that using postgres rather than MySQL before.

As previously mentioned you may want to just try the command directly into the database rather than going through PHP and see what messages you get.