Page 1 of 1

A PHP/MySQL question ...

Posted: Tue Jun 04, 2002 9:37 am
by ccjob2
I'm working on local apache web server. When I run the script below it doesn't insert noting into database.
I think the script is correct ... perhaps MySQL is bad configured ?
I use EasyPHP.

That's form input:

<html>
<head>
<title>Prova Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<?php
global $nome;
?>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form" method="get" action="dbase2.php">
<p>Nome:
<input type="text" name=nome size="40" maxlength="40">
</p>
<p>Cognome:
<input type="text" name=cognome size="45" maxlength="45">
</p>
<p>email:
<input type="text" name=email size="34" maxlength="34">
</p>
<p>
<input type="submit" name="Submit" value="Invia">
<input type="reset" name="reset" value="Reimposta">
</p>
</form>
</body>
</html>

That below is INSERT sript:

<HTML>
<HEAD>
<TITLE>prova database</TITLE>
</HEAD>
<BODY>
<?php

// variabili di connessione
// nome server, nome utente,
// Password, nomedatabase, tabella
// nel precedente articolo non abbiamo
// dato nessuna UID e PWD
$ServerName = "localhost";
$UserName = "root";
$Password = "";
$DbName = "utenti_db";
$TableName = "utenti";

// la connessione al database e alla tabella
$MyConn = mysql_connect($ServerName, $UserName, $Password )
or die ("Connessione fallita sul server $ServerName<br>");
$MyDb = mysql_select_db ($DbName, $MyConn)
or die ("Selezione del database fallita su $DbName<br>");

// istruzione SQL di selezione dei dati
$MyVarSQL = "INSERT INTO $TableName (id, nome, cognome, email) VALUES ('', '$nome', '$cognome', '$email')";
$MyQuery = mysql_query ($MyVarSQL, $MyConn)
or die ("Query di selezione fallita $MyVarSQL<br>");

// estrazione dei dati
// while($MyValues = mysql_fetch_array ($MyQuery))
// {
// $id = $MyValues["id"];
// $nome = $MyValues["nome"];
// $cognome = $MyValues["cognome"];
// $email = $MyValues["email"];
// echo "$id &nbsp;&nbsp;";
// echo "$nome &nbsp;&nbsp;";
// echo "$cognome &nbsp;&nbsp;";
// echo "$email &nbsp;&nbsp;";
// echo "<br>";
// }

// chiusura della connessione
mysql_close($MyConn);
?>
</BODY>
</HTML>

Thanks

Carlo

Posted: Tue Jun 04, 2002 9:48 am
by cwcollins
try this...

change:

Code: Select all

$MyVarSQL = "INSERT INTO $TableName (id, nome, cognome, email) VALUES ('', '$nome', '$cognome', '$email')";
to:

Code: Select all

$MyVarSQL = "INSERT INTO $TableName (nome, cognome, email) VALUES ('$nome', '$cognome', '$email')";
this may be part of it... are you getting an error mesage? is id an autonumber field? are their other fields in the table? will they accept null values?

c.w.collins

Posted: Tue Jun 04, 2002 10:32 am
by Johnm
echo out or kill your sql statement:

Code: Select all

// Use this
echo $MyVarSQL."\n";
 
// Or this

die($MyVarSQL );
and the actual sql statement will be outputted to your screen. Copy the statement and put it nativly into the database and see what errors occur. I am not sure with MySql, but in informix the db will return an error and if you use the built-in editor it will show you exactly where it is. I assume MySql has somthing like this.

Hope that helps.
Direwolf

Posted: Tue Jun 04, 2002 4:34 pm
by volka
yes, it has. change every

Code: Select all

$result=mysql_query($query,$conn);
to

Code: Select all

print("<!-- $query -->");
$result=mysql_query($query,$conn) or die(mysql_error());

Solved !!

Posted: Fri Jun 07, 2002 2:19 am
by ccjob2
The problem is php version 4.2.
Into this version POST and GET are not automatic variables (e.g. input: email, address, name=$email, $address, $name) but the code generate an array ($_POST['email'], $_POST['address'], etc.).
In that case the variables doesn't exists ...
The solution should be assign every element of the array to new variables (e.g. $emaildb=$_POST['email'], ecc.) and use the new variables into INSERT query.
Another question is how to standardize the script for all versions of PHP (Perhaps with an IF that verifies the version and run specifics codes)

Regards
Carlo

Posted: Fri Jun 07, 2002 2:23 am
by twigletmac
I can't believe nobody here spotted that :oops: . Maybe you become immune to a problem if you see it too many times.

ccjob2: There's a function called phpversion() that you can use to determine the version of PHP currently running.

Mac

Helping script

Posted: Fri Jun 07, 2002 3:42 am
by ccjob2
I found this script. I think is usefull ...

<?
///////////////////////////////////////////////////////////////////////////////
// php4-1-0_varfix.php January 09, 2001
// by Tom Harrison (thetomharrison@hotmail.com)
//
// According the the PHP Changelog, the version 4.1.0 release of PHP contains
// a drastic change in the way form, cookie and server values are made
// available. Instead of the old way
// (file.php?myvar=foobar yielding $myvar = "foobar"), such values are only
// assigned to associative arrays ($_GET, $_POST, $_COOKIE, $_SERVER
// and $_ENV). This has the effect of not only deprecating the $HTTP_*_VARS
// arrays and $fieldname = "fieldvalue" variables, but also
// voiding hundreds, if not thousands of existing web applications. As of
// 4.1.1, the $HTTP_*_VARS variables still exist, but the
// $fieldname = "fieldvalue" variables are completely gone.
//
// In an effort to preserve backwards compatability, this script cycles through
// these new structures and creates variables out of the field values. This
// means if you include this script at the top of your own scripts and run them
// on php 4.1.0, $yourformvalue or $yourcookievalue will contain the value
// you're expecting instead of nothing (as is the case if you ran your script
// without some kind of fix like this).
//
// The entire situation can be avoided by enabling register_globals. In 4.1.0,
// register_globals is deprecated but still on by default. In 4.1.1 however, it
// is off by default. This snippet is intended for those who don't have control
// over php's settings (such as virtually hosted sites) and need a quick fix
// while they transition their scripts to this arguably more secure method
// of making data available.
//
// For more information, see http://www.php.net/ChangeLog-4.php
///////////////////////////////////////////////////////////////////////////////

if (isset($_REQUEST)) {
while(list($varname, $varvalue) = each($_REQUEST)) { $$varname = $varvalue; }
}
if (isset($_SERVER)) {
while (list($varname, $varvalue) = each($_ENV)) { $$varname = $varvalue; }
while (list($varname, $varvalue) = each($_SERVER)) { $$varname = $varvalue; }
}

/*
There is no use yet for this function, but is included in anticipation of the
possibility of the $HTTP_*_VARS being fully deprecated.

function create_HTTP_VARS($type)
{
$temp = array();
switch(strtoupper($type))
{
case 'POST': $temp2 = &$_POST; break;
case 'GET': $temp2 = &$_GET; break;
case 'COOKIE': $temp2 = &$_COOKIE; break;
case 'SERVER': $temp2 = &$_SERVER; break;
case 'ENV': $temp2 = &$_ENV; break;
default: return 0;
}
while (list($varname, $varvalue) = each($temp2)) {
$temp[$varname] = $varvalue;
}
return ($temp);
}

if (!isset($HTTP_POST_VARS)) {
$HTTP_POST_VARS = create_HTTP_VARS('POST');
$HTTP_GET_VARS = create_HTTP_VARS('GET');
$HTTP_COOKIE_VARS = create_HTTP_VARS('COOKIE');
$HTTP_SERVER_VARS = create_HTTP_VARS('SERVER');
$HTTP_ENV_VARS = create_HTTP_VARS('ENV');
}
*/
?>

Bye Carlo