Jump to content

Mysql PHP program not running!


imtaqi

Recommended Posts

Following is my insert.php file to which my form post the data

 

<?php

$fname = $_POST["fname"];

$lname = $_POST["lname"];

$age = $_POST["age"];

$con = mysql_connect("localhost","giftme@1","mypass");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("giftme@1-airtime", $con);

$sql = "SELECT * FROM PERSON WHERE(first_name='$fname' AND last_name='$lname' AND age='$age')";

$result = MySQL_query($sql, $con);

$already_there = (MySQL_num_rows($result) > 0);

 

if ($already_there) {

    $array = MySQL_fetch_array($result);

    $id = $array['id'];

    $entries = $array['entries'] + 1;

    $sql = "UPDATE person SET entries=$entries WHERE id=$id";

}

else {

    $sql = "INSERT INTO person VALUES('', '$fname', '$lname', 1)";

}

MySQL_query($sql, $con);

MySQL_close($con);

 

/*redirect - this must be the first output of the script

*not even an empty line can come before the initial <?php tag

*/

header('location: http://yahoo.com/');

?>

 

I dont know what's gone wrong with the program not running giving following error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /data/apache/users/kilu.de/giftme/www/form/insert.php on line 14

 

Warning: Cannot modify header information - headers already sent by (output started at /data/apache/users/kilu.de/giftme/www/form/insert.php:14) in /data/apache/users/kilu.de/giftme/www/form/insert.php on line 31

?>

 

Line 14: $already_there = (MySQL_num_rows($result) > 0);

Line 31: header('location: http://yahoo.com/');

 

Could any1 help :D

Link to comment
https://forums.phpfreaks.com/topic/87089-mysql-php-program-not-running/
Share on other sites

do yourself a few MASSIVE favours... 1) dont use static variables... 2) use errors!!!

 

<?php
$age = $_POST["age"];
$con = mysql_connect("localhost","giftme@1","mypass") or die('Could not connect: ' . mysql_error());
mysql_select_db("giftme@1-airtime", $con);
$result = MySQL_query("SELECT * FROM `PERSON` WHERE(`first_name`='$fname' AND `last_name`='$lname' AND `age`='$age') LIMIT 1") or die(mysql_error());
if(mysql_num_rows($result) > 0){
$array = mysql_fetch_array($result);
$entries = ;
mysql_query("UPDATE `person` SET `entries`='{$array['entries']+ 1}' WHERE `id`='{$array[id]}'");
}else {
mysql_query("INSERT INTO `person` VALUES('', '{$_POST[fname]}', '{$_POST["lname"]}', 1)");
}
mysql_close($con);

header('location: http://yahoo.com/');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.