Jump to content

Recommended Posts

I am having a small problem with my sql statement. it is inserting into 4 of the fields.

 

fields it is inserting into:

First

Last

email

password

 

it is saying the record has been added. is there something wrong with the sql statement?

 

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="95887rj"; // Mysql password 
$db_name="bccsl"; // Database name 
$tbl_name="managers2"; // 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");

$sql = sprintf("INSERT INTO Managers (First, Last, Address, City, Postal, Home, Cell, Email, Password, Church, Team)VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$_POST['firstname'],$_POST['lastname'],$_POST['address'],$_POST['city'],$_POST['postal'],$_POST['home'],$_POST['cell'],$_POST['email'],$_POST['password'],$_POST['church'],$_POST['team']);

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

?> 

Link to comment
https://forums.phpfreaks.com/topic/183757-sql-problems/
Share on other sites

this is what i am currently seeing. my form is fine. and every field has data "test" in it. i am new to php and trying to learn it am i missing something?

 

1 record added INSERT INTO Managers (First, Last, Address, City, Postal, Home, Cell, Email, Password, Church, Team)VALUES ('test','test','','','','','','test','test','','') 

Link to comment
https://forums.phpfreaks.com/topic/183757-sql-problems/#findComment-969976
Share on other sites

Query runs fine (it inserts the values after all), so there will be no MySQL error.

 

Instead could you please show us your form code? There might be something in there. Perhaps fields in form are named differently than POST variables you use.

I was thinking along the lines of misspelled column, even though the query looks like it would work.

Link to comment
https://forums.phpfreaks.com/topic/183757-sql-problems/#findComment-970225
Share on other sites

Here is my form. it is basic right now to get it working then i will CSS it to death.

 

<html>
<head>
<title>manager add</title>
</head>
<body>

<form action="insert.php" method="post" name="manger" id="manger">
  <p>Firstname: <input type="text" name="firstname" />
  </p>
  <p>
    Lastname: <input type="text" name="lastname" />
  </p>
  <p>
    Address <input type="text" name="Address" id="address">
    </p>
  <p>
    City <input type="text" name="City" id="city">
  </p>
  <p>
    Postal <input type="text" name="Postal" id="postal">  
  </p>
  <p>
    Home <input type="text" name="Home" id="home">
  </p>
  <p>
    Cell <input type="text" name="Cell" id="cell"> 
  </p>
  <p>
    Email <input type="text" name="email" id="email">
  </p>
  <p>
    Password <input type="text" name="password" id="password">
  </p>
  <p>
    Church <input type="text" name="Church" id="church">
  </p>
  <p>
    Team Name <input type="text" name="TeamName" id="team">
  </p>
  <p>
    <input type="submit" />
            </p>
</form>

</body>
</html>

 

sql insert:

 

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="95887rj"; // Mysql password 
$db_name="bccsl"; // Database name 
$tbl_name="managers2"; // 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");

$sql = sprintf("INSERT INTO Managers (First, Last, Address, City, Postal, Home, Cell, Email, Password, Church, Team)VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",$_POST['firstname'],$_POST['lastname'],$_POST['address'],$_POST['city'],$_POST['postal'],$_POST['home'],$_POST['cell'],$_POST['email'],$_POST['password'],$_POST['church'],$_POST['team']);

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

$sql";

?> 

Link to comment
https://forums.phpfreaks.com/topic/183757-sql-problems/#findComment-970236
Share on other sites

I guess I'll post this one more time -

 

You should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you. (Confirm the actual settings using a phpinfo() statement in case the php.ini that you are changing is not the one that php is using.)

 

You will save a ton of time. In this case you could have probably saved a whole day of your time because you would have gotten an undefined index error concerning the mismatch in the variable name that would have alerted you to which one(s) did not match what the form was sending.

Link to comment
https://forums.phpfreaks.com/topic/183757-sql-problems/#findComment-970744
Share on other sites

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.