Jump to content
Sign in to follow this  
pritam79

insert error

Recommended Posts

I have two pages ‘register_user.php’ which allows job-applicants to register, and ‘register_company.php’ which allows the companies to register. I am using wamp. A job-applicant or Company gets registered properly, but the only errors I get are-

  • level_usr gets inserted in the tables as 0 in both cases whereas it should have inserted a 1 for an applicant, and 2 for a company.
  • Also, the email field value of the table is inserted as - it begins with some weird character and then the address followed by a dot, but the com after the dot is absent.

Edited by pritam79
  • Like 1

Share this post


Link to post
Share on other sites
can you show the part of your code which has the insert query
This is register_company.php<?phpsession_start();if(isset($_SESSION['id_usr'])){  if($_SESSION['level_usr']=1)	 {	 header('Location:user/index.php');	 exit;	 }  if($_SESSION['level_usr']=2)	 {	 header('Location:company/index.php');	 exit;	 }}if($_SERVER['REQUEST_METHOD'] == 'POST'){  if(strlen($_POST['txtusername']) !='' && strlen($_POST['txtpassword']) !='' && strlen($_POST['txtemail']) !='')   {    require("db-connect.php");    $result = db_connect();    if($result)	 {	  $username = mysql_real_escape_string($_POST['txtusername'], $result);	  $password = mysql_real_escape_string($_POST['txtpassword'], $result);	  $email = mysql_real_escape_string($_POST['txtemail'], $result);	  $sql = "INSERT INTO user_usr (id_usr, username_usr, password_usr, level_usr, email_usr)			    VALUES ('', '$username', '$password', '2', '$email')";	  mysql_query($sql, $result) or die(mysql_error($result));	  $_SESSION['id_usr'] = mysql_insert_id($result);	  $_SESSION['level_usr'] = 2;	  $_SESSION['username'] = $username;	 	  $sql = "INSERT INTO company_com (id_com, idusr_com) VALUES ('',  '".$_SESSION['id_usr']."')";	  mysql_query($sql, $result) or die(mysql_error($result));	  $_SESSION['id_com'] = mysql_insert_id($result);	     //  $sql = "INSERT INTO company_com (id_com, idusr_com) VALUES ('',  '".$_SESSION['id_usr']."')";   //   mysql_query($sql, $result) or die(mysql_error($result));    //  $sql = "INSERT INTO job_job (id_job, idcom_job) VALUES ('', '".$_SESSION['id_com']."')";//	  mysql_query($sql, $result) or die(mysql_error($result));	 }	 header('Location: company/index.php');	 exit();    }  } include "header.php";?><body><div id="navbar"><table align="center"><tr>  <td><a href="index.php">Home</a></td>  <td><a href="about_us.php">About Us</a></td>   <td><a href="register_user.php">Register User</a></td>  <td><a href="register_company.php">Register Company</a></td>  <td><a href="login.php">Login</a></td></tr></table></div><div id="content"><br><div id="msg">Fill in your details below to register as a company</div><br><form name="company_registration_form" method="post" action="register_company.php"><table align="center"><tr>  <td>Username:</td><td><input type="text" name="txtusername" value="<?php if(isset($_POST['submit']) && (strlen($_POST['txtusername']) > 0 || strlen($_POST['txtusername']) < 7)) echo $_POST['txtusername']; ?>"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtusername']) < 1 || strlen($_POST['txtusername']) >6)) echo "<div id='rusr-err'>Username must be 1 to 6 chars<div>"; ?></tr><tr>  <td>Password:</td><td><input type="password" name="txtpassword"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtpassword']) < 1 || strlen($_POST['txtpassword']) >6)) echo "<div id='rpas-err'>Password must be 1 to 6 chars<div>"; ?></tr><tr>  <td>E-mail:</td><td><input type="text" name="txtemail" value="<?php if(isset($_POST['submit']) && isset($_POST['txtemail'])) echo $_POST['txtemail']; ?>"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtemail']) < 10 || strlen($_POST['txtemail']) > 25)) echo "<div id='remail-err'>Email must be at least 14 chars<div>"; ?></tr><tr>  <td></td><td align="center"><input type="submit" value="Submit" name="submit"> <input type="reset"></td></tr></table></form></div><?phpinclude "footer.php";?>

This is register_user.php

<?phpsession_start();if(isset($_SESSION['id_usr'])){  if($_SESSION['level_usr']=1)	 {	 header('Location:user/index.php');	 exit;	 }  if($_SESSION['level_usr']=2)	 {	 header('Location:company/index.php');	 exit;	 }}if($_SERVER['REQUEST_METHOD'] == 'POST'){  if(strlen($_POST['txtusername']) !='' && strlen($_POST['txtpassword']) !='' && strlen($_POST['txtemail']) !='')   {    require("db-connect.php");    $result = db_connect();    if($result)	 {	  $username = mysql_real_escape_string($_POST['txtusername'], $result);	  $password = mysql_real_escape_string($_POST['txtpassword'], $result);	  $email = mysql_real_escape_string($_POST['txtemail'], $result);	 	  $sql = "INSERT INTO user_usr (id_usr, username_usr, password_usr, level_usr, email_usr)			  VALUES ('', '$username', '$password', '1', '$email')";	  mysql_query($sql, $result) or die(mysql_error($result));	  $_SESSION['id_usr'] = mysql_insert_id($result);	  $_SESSION['level_usr'] = 1;	  $_SESSION['username'] = $username;	 }	 header('Location: user/index.php');	 exit();    }  }include "header.php";?><body><div id="navbar"><table align="center"><tr>  <td><a href="index.php">Home</a></td>  <td><a href="about_us.php">About Us</a></td>   <td><a href="register_user.php">Register User</a></td>  <td><a href="register_company.php">Register Company</a></td>  <td><a href="login.php">Login</a></td></tr></table></div><div id="content"><br><div id="msg">Fill in your details below to register as an applicant</div><br><form name="user_registration_form" method="post" action="register_user.php"><table align="center"><tr>  <td>Username:</td><td><input type="text" name="txtusername" value="<?php if(isset($_POST['submit']) && (strlen($_POST['txtusername']) > 0 || strlen($_POST['txtusername']) < 7)) echo $_POST['txtusername']; ?>"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtusername']) < 1 || strlen($_POST['txtusername']) >6)) echo "<div id='rusr-err'>Username must be 1 to 6 chars<div>"; ?></tr><tr>  <td>Password:</td><td><input type="password" name="txtpassword"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtpassword']) < 1 || strlen($_POST['txtpassword']) >6)) echo "<div id='rpas-err'>Password must be 1 to 6 chars<div>"; ?></tr><tr>  <td>E-mail:</td><td><input type="text" name="txtemail" value="<?php if(isset($_POST['submit']) && isset($_POST['txtemail'])) echo $_POST['txtemail']; ?>"></td><?php if(isset($_POST['submit']) && (strlen($_POST['txtemail']) < 10 || strlen($_POST['txtemail']) > 25)) echo "<div id='remail-err'>Email must be at least 14 chars<div>"; ?></tr><tr>  <td></td><td align="center"><input type="submit" value="Submit" name="submit"> <input type="reset"></td></tr></table></form></div><?phpinclude "footer.php";?>

Share this post


Link to post
Share on other sites

if your "level_usr" field in SQL has a numeric type (integer, tinyint etc), remove the apostrophes from around the '2' and the '1' in the insert queries. for the email field, make sure the length for the field is set high enough, otherwise characters will get cut off the end.

Share this post


Link to post
Share on other sites
if your "level_usr" field in SQL has a numeric type (integer, tinyint etc), remove the apostrophes from around the '2' and the '1' in the insert queries. for the email field, make sure the length for the field is set high enough, otherwise characters will get cut off the end.
I removed the apostrophes from ‘1’ and ‘2’ (int type) of the two insert queries, but even now only zeros are getting inserted in MySql. But when I echo $_SESSION['level_usr'] on the page where a user is redirected after registration, I get the proper output as 1 or 2 depending on who registered (user or company). Its showing 0 in the table evertime I register be it a user or a company.Also, the length of the email field is varchar(25), but still the problem persists.

Share this post


Link to post
Share on other sites

try to print $sql.what does it print?

Share this post


Link to post
Share on other sites
try to print $sql.what does it print?
I tried the following for user registration.
$sql = "INSERT INTO user_usr (id_usr, username_usr, password_usr, level_usr, email_usr)			  VALUES ('', '$username', '$password', '1', '$email')";	  mysql_query($sql, $result) or die(mysql_error($result));	  echo $sql;	  exit;

The user was registered as before with the values getting inserted into MySql as earlier. But on echoing $sql I get this output in the browser window-INSERT INTO user_usr (id_usr, username_usr, password_usr, level_usr, email_usr) VALUES ('', 'User7', 'User7', '1', 'user7@gmail.com')

Share this post


Link to post
Share on other sites

you are still quoting around integer. unquote it in user level. I am not sure about email field. it seems like it should be ok.

Share this post


Link to post
Share on other sites

its all working now. just had some problem with the database table structure. Now when I register, I am trying to execute multiple insert queries at the same time as given below.

$sql = "INSERT INTO company_com (id_com, idusr_com) VALUES ('',  '".$_SESSION['id_usr']."')";	  mysql_query($sql, $result) or die(mysql_error($result));	  $_SESSION['id_com'] = mysql_insert_id($result);	 	  $sql = "INSERT INTO domain_dom (id_dom) VALUES ('')";	  mysql_query($sql, $result) or die(mysql_error($result));	 	  $sql = "INSERT INTO location_loc (id_loc) VALUES ('')";	  mysql_query($sql, $result) or die(mysql_error($result));	  $sql = "INSERT INTO job_job (id_job, idcom_job, iddom_job, idloc_job) VALUES ('', '".$_SESSION['id_com']."', '".$_SESSION['id_dom']."', '".$_SESSION['id_loc']."')";	  mysql_query($sql, $result) or die(mysql_error($result));

But i get the error - Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\Job-site\register_company.php on line 45. Any help in identifying it?

Share this post


Link to post
Share on other sites

it means there is a variable where it should not supposed tonbe. check on line 45 and above

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...