FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


cannot add data to mysql from PHP form





adeydas
hi,

i am trying to add data to a mysql table with PHP. the data is taken from a form. though it can connect to the server and select the database, it cannot insert data into the database. however, the script works fine on a local server. here's the script: http://adeydas.com/download.php?file=reg-submit&ext=txt

The code as in the above link:
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Results</title>
<link href="advaita/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<h3>
<?php
//This script is used to submit the registration form

//Info from the registration form is accepted here and checked for accuracy
$email = $_POST['emailid']; //for the email id
$contact = $_POST['contactno']; //for the contact number
$coll = $_POST['college']; //for the college name
$cit = $_POST['city']; //for the city name or village name if its gunupur

$tsize = $_POST['teamsize']; //size of the team
$noofboys = $_POST['numberofboys']; //for the number of boys
$noofgirls = $_POST['numberofgirls']; //for the number of girls
$nameparticipants = $_POST['nameofparticipants']; //name of participants
$event = $_POST['events']; //events participating in
//$datetime = $_POST['date_and_time']; //date and time
$date1 = $_POST['date'];
$month1 = $_POST['month'];
$year1 = $_POST['year'];
$coll2 = $_POST['am4mcoll'];
$q = $_POST['query']; //queries

//echo($event);

//Now that we have all the data, lets check for accuracy

include('functions.php');

//echo($coll);
if ($coll2)
{
$datetime = "He/She is a GIETian";
}
else
{
$datetime = $date1."-".$month1."-".$year1;
}

//checking emaill addy for accuracy
if(empty($email) || !$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{
echo("You didn't enter a correct email!!! ");
//echo($email);
die ("Go back! ! ");
}

//check for contact no
if (empty($contact) || !isDigits($contact))
{
echo("Enter a contact number");
die ("Go back! ! ");
}


//check for college
if (empty($coll) || !isLetters($coll))
{
echo("Enter your college name correctly");
die ("Go back! ! ");
}

//check for city
if (empty($cit) || !isLetters($cit))
{
echo("Enter a city name");
die ("Go back! ! ");
}

//checking team size
if (empty($tsize) || (int)$tsize > 20 || !isDigits($tsize))
{
echo("Team size too big, empty or incorrect entry!!! ");
die ("Go back! ! ");
}

if (!isDigits($noofboys))
{
echo("Specify No Of Boys Correctly!!! ");
die ("Go back! ! ");
}

if (empty($noofboys))
{
$noofboys="NULL";
}

if (!isDigits($noofgirls))
{
echo("Specify No Of Girls Correctly!!! ");
die ("Go back! ! ");
}

if (empty($noofgirls))
{
$noofgirls="NULL";
}

if ($noofboys!="NULL" && $noofgirls!="NULL")
{
$total = $noofboys+$noofgirls;

if ($total != $tsize)
{
echo("Number of boys and girls entered does not match with the team size!!! ");
die("Go Back ! !");
}
}

if (empty($datetime))
{
$datetime="NULL";
}

if (empty($q))
{
$q="NULL";
}

//checking team participants
if (empty($nameparticipants))
{
echo("You must enter the name of atleast one team participant!!!");
die ("Go back! ! ");
}

//checking events
if (empty($event))
{
echo("Cmon friend. You can participate in atleast one event!!!");
die();
}

include('config.php');
include('opendb.php');
//All the cool code goes here


$query2 = "SELECT rid, remail, events FROM ad_reg08 WHERE remail='$email'";
$sql3 = mysql_query($query2, $conn);
while($row2 = mysql_fetch_array($sql3, MYSQL_ASSOC))
{
    $iid2 = $row2['rid'];
   $email2 = $row2['remail'];
   $event6 = $row2['events'];
}

//echo($iid2);

if ($iid2=="")
{
$query = "INSERT INTO ad_reg08(remail, contactno, college, city, team_size, noboys, nogirls, nameparticipants, events, datetimearrive, query)
VALUES ('$email','$contact','$coll','$cit','$tsize','$noofboys','$noofgirls','$nameparticipants','$event','$datetime','$q')";

//echo($query);

$sql1 = mysql_query($query, $conn);

if (!$sql1)
{
echo("Couldn't insert your data into the Advaita database!!! Please contact the webmaster at webmaster@advaitagiet.com");
}
else
{
echo("Data added to Advaita database!!!");
}

$query = "SELECT rid FROM ad_reg08 WHERE remail='$email'";
$sql2 = mysql_query($query, $conn);

while($row = mysql_fetch_array($sql2, MYSQL_ASSOC))
{
    $iid = $row['rid'];
}

if (!$sql2)
{
echo("Couldn't read data from Advaita database!!! Sorry we can't provide you with your registration id at the present moment");
}
else
{
echo("<br />Your Registration ID is ad-$iid.<br /> Please email your abstract to    literary@advaitagiet.com<br />For any further correspondence please mention your registration ID and email.");
$msg = "Thank you for participating in <strong>Advaita 2008</strong>, the Annual Tech Fest of GIET, Gunupur.<br />. Your Registration ID is <strong>ad-$iid</strong>. If you wish to update your registration info, please use the same email to register again with the updated information.<br /><br />Wishing you a happy stay at Gunupur, <br />The Advaita Team.";
mail($email, "Advaita 2008 Registration information", $msg, "webmaster@advaitagiet.com");
echo("<br />An email has been sent to your mail account with the registration information");
}

}
else
{
echo("You have already registered on that email with ID: ad-"); echo($iid2);
echo("<br />So we will just update your record with the new values!!!");

$event=$event.",".$event6;

$query9 = "UPDATE ad_reg08 SET remail='$email',contactno='$contact',college='$coll',city='$cit',team_size='$tsize',noboys='$noofboys',nogirls='$noofgirls',nameparticipants='$nameparticipants',events='$event',datetimearrive='$datetime',query='$q' WHERE remail='$email2'";

//echo($query9);

$rr = mysql_query($query9, $conn);

if (!$rr)
{
echo("<br />Couldn't update Advaita database!!!");

}
else
{
echo("<br />Your record has been updated successfully!!!<br />For any further correspondence please mention your registration ID and email.");
$msg = "Thank you for participating in <strong>Advaita 2008</strong>, the Annual Tech Fest of GIET, Gunupur.<br />. Your Registration ID is <strong>ad-$iid</strong>. Your registration info has been updated successfully. If you wish to update your registration info further, please use the same email to register again with the updated information.<br /><br />Wishing you a happy stay at Gunupur, <br />The Advaita Team.";
mail($email, "Advaita 2008 Registration information", $msg, "webmaster@advaitagiet.com");
$msg2 = "Account ad-$iid has been update. Please look in it.";
mail("webmaster@advaitagiet.com","Account Updated",$msg2);
echo("<br />An email has been sent to your mail account with the updated registration information");
}
}

include('closedb.php');
?>
<p><a href="javascript:window.back();">Return</a></p>
</h3>
</center>
</body>
</html>


thanks!!!
Diablosblizz
Possibly try adding the MySQL connection code instead of doing $conn in the queries. I doubt that'll make a difference, but try it anyways?
imagefree
which error message you are getting?

Is it "Couldn't insert your data into the Advaita database!!! Please contact the webmaster at webmaster@advaitagiet.com" ?


Edit:


Is it working properly?


Code:
while($row2 = mysql_fetch_array($sql3, MYSQL_ASSOC))
{
    $iid2 = $row2['rid'];
   $email2 = $row2['remail'];
   $event6 = $row2['events'];
}
carl_fernandes
Hi,

I was going through you code. Dont know if this is the cause of the error but i noticed the following line in your code "$sql1 = mysql_query($query, $conn); ". Is the $conn variable the same an the one which is used while making the connection to the db. Otherwise try using this "$sql1 = mysql_query($query); ". The $conn is not necessary in you case here if you using only one database.
Related topics
How To : Secure Your PHP Website
A very good PHP MySQL Tutorial
Adding field to message data
Uplaoding images using php form :::: Help needed
Php form.
MySQL, PHP and APACHE
How to search in a MySQL - database
Simple MySQL Template
Is mySQL/php in safe mode on Frihosting?
PHP Form Question
800$ frih for grab, php form small correction - urgent
php and mysql form
SQL doesn't add data [SOLVED]
Form handling in php is a pain in the neck
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.