MySql Error

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
ZulfadlyAshBurn
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:51 pm

MySql Error

Post by ZulfadlyAshBurn »

I have this code. but i dunno why it doesnt insert the data to my mysql database. everything is correct. anyone can help me point me the problem?
the email is send out and the folder is created but the data is not save to the database :(

the domain have been change for security purpose. thanks in advance ;D

Code: Select all

<?php
$host="localhost"; // Host name
$username="zulfadly_code"; // Mysql username
$password="lololosadsad"; // Mysql password
$db_name="zulfadly_code"; // Database name
$tbl_name="member"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$clas=$_POST['clas'];
$desc=$_POST['desc'];
$folderurl=$_POST['folderurl'];
$vpass=$_POST['vpass'];
$email=$_POST['email'];


$sql="SELECT * FROM $tbl_name WHERE username='$myusername'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Check if Username taken, if taken then echo else insert
  print "Username Taken! <a href='register.php'>Retry</a>";
  exit;
}

//check if passwords are the same
        if ($mypassword != $vpass) {
  echo "The passwords do not match. <a href='register.php'>Retry</a>";
  exit;
        }
else {

$dir = 'u//'. $_POST[folderurl]; //see the double \\ 
if(!is_dir($dir)) 
{ 

  $email = $_REQUEST['email'] ;
  $message = "Thank you for registering! Below are the details for your registration. \n\nUsername: $myusername \n Password: $mypassword \n Url: http://www.example.com/ons/u/ $folderurl";

  mail( "$email", "Registration Info",
    $message, "From: admin@example.com" );

mysql_query("INSERT INTO member (id, username, password, class, desc, email)
VALUES ('', '$myusername', '$mypassword', '$clas', '$desc', '$email')");

echo "Thank you for registering! We have sent an message to your email regarding your registration info. If you have not received, please check your junk mailbox. Click <a href='index.html'>here</a> to login!";

mkdir($dir); 
echo "Sub Folder Successfully Created!"; 
}
else {
echo "Url Taken!";
}
}
?>
mrcniceguy
Forum Newbie
Posts: 3
Joined: Mon Dec 20, 2010 8:59 am

Re: MySql Error

Post by mrcniceguy »

i havent passed through all your code,but it seems OK.

Try to check if all the the fields are in the database
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: MySql Error

Post by mikosiko »

My first suggestion for you is to enable the display of errors in your code... add this 2 lines at the begining of your script

Code: Select all

<?php
   error_reporting(E_ALL); 
   ini_set("display_errors", 1);
.... < rest of your code>
...
that should allow you to see that you have an error in your INSERT

Code: Select all

mysql_query("INSERT INTO member (id, username, password, class, desc, email)
VALUES ('', '$myusername', '$mypassword', '$clas', '$desc', '$email')");
because you are using "desc" as a column name, and "desc" is a mysql reserved word
http://dev.mysql.com/doc/refman/5.1/en/ ... words.html

the best solution is to change the field name in your table, but if that is not possible you can enclose it in backtick like `desc`
ZulfadlyAshBurn
Forum Newbie
Posts: 2
Joined: Mon Dec 20, 2010 12:51 pm

Re: MySql Error

Post by ZulfadlyAshBurn »

@mikosiko Thanks Alot. It finally work properly. I change the field name to mydesc and it works. did some editing to the reguser.php though. thanks :D
Post Reply