Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

database not inserting properly

Links for php scripts

Moderators: macek, egami, gesf

database not inserting properly

Postby sujskiez » Thu Jun 12, 2014 8:59 pm

So i have this html page with php code linked with .htaccess but when i try to insert some values from the form into the database. So when i try to indert into the database the values aren't going in but the only value that is going in are the numbers 1.
Can someone help me?? this is the code

Code: Select all
<!DOCTYPE HTML>
<html>

<head>
<script language="javaScript" src="myfile.php"></script>
<?php

     error_reporting(E_ALL ^ E_DEPRECATED);
     $con = mysql_connect("localhost","root","");
   if (!$con)
     {
      die('Could not connect: ' . mysql_error());
     }
    mysql_select_db("whatchadoin", $con);
   
   //$f = "Heelo";
   
   $last_name = isset($_POST['lname']);
   $email = isset($_POST["email"]);
   //$gender = $_POST['sel_gender'];
   if(isset($_POST['fname']) && isset($_REQUEST['submit']))
   {
   
   $f_name = isset($_POST['fname']);
   $sql="INSERT INTO participant (fname, lname,email) VALUES
   ('$f_name','$last_name','$email')";

   if (!mysql_query($sql,$con))
     {
     die('Error: ' . mysql_error());
     }else
   echo "1 record added";
   }
   

   mysql_close($con);
   


?>
</head>

<body>
<form  method='post' >
<input type="text" name="fname" id="fname" />
<input type="text" name="lname" id="lname" />
<input type="text" name="email" id="email" />


<input type="submit" name="submit" value="submit"/>
</form>






</body>
</html>
sujskiez
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Jun 12, 2014 8:49 pm

Re: database not inserting properly

Postby freshnet » Sat Oct 18, 2014 4:41 pm

you're using isset() to set a variable. Isset() is a function that returns true (i.e. 1) if a variable is set. So if your variables are set, you're changing them all to 1. You probably don't even need to test if it's set, because if it's not, you'll just get nothing inserted into the field.

Also, the mysql extension is deprecated and you shouldn't be using it. Use mysqli (very similar) or PDO (much more secure) instead.

Code: Select all
error_reporting(E_ALL ^ E_DEPRECATED);
     $con = mysql_connect("localhost","root","");
   if (!$con)
     {
      die('Could not connect: ' . mysql_error());
     }
    mysql_select_db("whatchadoin", $con);
   
   //$f = "Heelo";
   
   $last_name = $_POST['lname'];
   $email = $_POST["email"];
   //$gender = $_POST['sel_gender'];
   if(isset($_POST['fname']) && isset($_REQUEST['submit']))
   {
   
   $f_name = $_POST['fname'];
   $sql="INSERT INTO participant (fname, lname,email) VALUES
   ('$f_name','$last_name','$email')";

   if (!mysql_query($sql,$con))
     {
     die('Error: ' . mysql_error());
     }else
   echo "1 record added";
   }
   

   mysql_close($con);
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 278
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron