Jump to content

Recommended Posts

I'm trying to take data from a form and send the information to my database. Whenever I try to submit my form, this script handles it. The error that I get is "Query was empty" whenever I try to submit my form. I'm thinking that it has something to do with my sql insert portion, but can't figure out what I am doing wrong. Any help is greatly appreciated.

 

<?php
// database information 
   $host = 'localhost';       
   $user = 'root'; 
   $password = 'mypass'; 
   $dbName = 'mydb'; 

   // connect and select the database 
   $conn = mysql_connect($host, $user, $password) or die(mysql_error()); 
   $db = mysql_select_db($dbName, $conn) or die(mysql_error()); 
   
   // Check to make sure that the fields weren't empty.
   if (isset($_POST['first_name']) != '') { 
      $first_name = stripslashes($_POST['first_name']);  
  }
  
if (isset($_POST['last_name']) != '') { 
      $last_name = stripslashes($_POST['last_name']); 
  }
  
if (isset($_POST['email']) != '') {  
      $email = stripslashes($_POST['email']); 
  }
  
  // Insert the 3 into the DB
  $sql="INSERT INTO 'users' (first_name, last_name, email) VALUES('$first_name','$last_name','$email')";
  $result = mysql_query($sql, $conn) or die(mysql_error());
   ?>

 

Thanks for reading,

Brandon

Link to comment
https://forums.phpfreaks.com/topic/86711-solved-inserting-string-to-db/
Share on other sites

Here is the form.php and the updated formhandle.php

 

<?php
// database information 
   $host = 'localhost';       
   $user = 'root'; 
   $password = 'mypass'; 
   $dbName = 'mydb'; 

   // connect and select the database 
   $conn = mysql_connect($host, $user, $password) or die(mysql_error()); 
   $db = mysql_select_db($dbName, $conn) or die(mysql_error()); 
   
   // insert new entry in the database if entry submitted 
   if (isset($_POST['first_name']) && trim($_POST['first_name']) != '') { 
      $first_name = $_POST['first_name']; 
      $result = mysql_query($sql, $conn) or die(mysql_error());  
  }
  
if (isset($_POST['last_name']) && trim($_POST['last_name']) != '') { 
      $last_name = $_POST['last_name']; 
  }
  
 if (isset($_POST['email']) && trim($_POST['email']) != '') {  
      $email = $_POST['email']; 
  }
  
  // Insert the 3 into the DB
  $sql="INSERT INTO users (first_name, last_name, email) VALUES(' {$first_name} ',' {$last_name} ',' {$email} ')";
  $result = mysql_query($sql, $conn) or die(mysql_error());
   ?>
   

 


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="formhandle.php">
  <p>
    
    First Name: 
    <input name="first_name" type="text" id="first_name">
  </p>
  <p>Last name: 
    <input name="last_name" type="text" id="last_name">
  </p>
  <p>Email: 
    <input name="email" type="text" id="email">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.