cant insert into mysql

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
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

cant insert into mysql

Post by acwe88 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi i have this script but it will not let me insert the data into mysql can anyone see were i am going wrong

Code: Select all

<html>
<head>
</head>
<body>

<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>


<table width="700" border="0" cellspacing="5">
  <tr>
    <td align="right">
    
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    
    Name:</td>
    
    <td><input type="text" name="name" size="30"></td>
  </tr>
  <tr>
    <td align="right">Surname:</td>
    <td><input type="text" name="surname" size="30"></td>
  </tr>
  <tr>
    <td align="right">Username:</td>
    <td><input type="text" name="userrname" size="30"></td>
  </tr>
  <tr>
    <td align="right">Headline:</td>
    <td><input type="text" name="headline" size="30"></td>
  </tr>
  <tr>
    <td align="right">About Me:</td>
    <td><input type="text" name="aboutme" height="30" size="30"></td>
  </tr>
  <tr>
    <td align="right">i'd like to meet:</td>
    <td><input type="text" name="meet" size="30"></td>
  </tr>
  <tr>
    <td align="right">Music:</td>
    <td><input type="text" name="music" size="30"></td>
  </tr>
  <tr>
    <td align="right">Films:</td>
    <td><input type="text" name="films" size="30"></td>
  </tr>
  <tr>
    <td align="right">T.V:</td>
    <td><input type="text" name="tv" size="30"></td>
  </tr>
  <tr>
    <td align="right">Books</td>
    <td><input type="text" name="books" size="30"></td>
  </tr>
  <tr>
    <td align="right">Heroes:</td>
    <td><input type="text" name="heroes" size="30"></td>
  </tr>
  <tr>
    <td></td> 
    <td><input type='submit' value='Submit'></td>
  </tr>
  </form>
</table>

<?php
}
else{
// form submitted
// set server access variables
$host = "*********";
$user = "**********";
$pass = "**********";
$db = "**********";

    // open connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($db) or die ("Unable to select database!");
    
    // create query
    $query = "INSERT INTO members_profile (name, surname, username, headline, aboutme, meet, music, films, tv, books, heroes) VALUES ('$name', '$surname', '$username', '$headline', '$aboutme', '$meet', '$music', '$films', '$tv', '$books', '$heroes')";
    
    // execute query
    $result = mysql_query($query)or die("Error in query: $query. ".mysql_error()); 
    
    // print message with ID of inserted record
    echo "New record inserted with ID ".mysql_insert_id();
    echo "<br><br><a href='#' onClick='history.go(-1)'>Back</a>";
    
    // close connection
    mysql_close($connection);
}
    
    
?>

</body>
</html>


I have the exact same script on another program and it works fine its also using the same server. The only thing different is I have put the html form into a table.

Thanks
Alex


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You've got comprehensive error reporting there, what does it say?
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

Post by acwe88 »

im not seeing any errors

its just going blank when i press submit and then clears the form

Thanks
AL
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

well the first thing I've noticed is that you aren't setting the post var 'submit' anywhere so, your

Code: Select all

if (!isset($_POST['submit'])) {
will always be true, and therefore your INSERT code will never run.

second point is that unless you have global variables turned on (which can be a security risk), you have not collected the data from the $_POST array. if global variables is turned off you would need to assign each of the vars from $_POST to your variables

example

Code: Select all

$name = $_POST['name'];
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

Post by acwe88 »

Solved thanks everyone for your help

Basically first of all the

Code: Select all

<input type='submit' name='submit' value='Submit'>
Needed a name

also all vars needed to be

$name = "$_post['name']";



Thanks guys and girls
Alex
acwe88
Forum Newbie
Posts: 24
Joined: Tue Nov 07, 2006 10:47 am

SOLVED

Post by acwe88 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


For those who would like the full script  here it is and it works like a charm:)

Apologies feyd but i have'nt got time to add the code properly

Code: Select all

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>

<html>
<head>
</head>
<body>

<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>


<table width="700" border="0" cellspacing="5">
  <tr>
    <td align="right">
    
  <form action="<?=$_SERVER['PHP_SELF']?>" method="post">  
    
    Name:</td>
    
    <td><input type="text" name="name" size="30"></td>
  </tr>
  <tr>
    <td align="right">Surname:</td>
    <td><input type="text" name="surname" size="30"></td>
  </tr>
  <tr>
    <td align="right">Username:</td>
    <td><input type="text" name="username" size="30"></td>
  </tr>
  <tr>
    <td align="right">Headline:</td>
    <td><input type="text" name="headline" size="30"></td>
  </tr>
  <tr>
    <td align="right">About Me:</td>
    <td><input type="text" name="aboutme" height="30" size="30"></td>
  </tr>
  <tr>
    <td align="right">i'd like to meet:</td>
    <td><input type="text" name="meet" size="30"></td>
  </tr>
  <tr>
    <td align="right">Music:</td>
    <td><input type="text" name="music" size="30"></td>
  </tr>
  <tr>
    <td align="right">Films:</td>
    <td><input type="text" name="films" size="30"></td>
  </tr>
  <tr>
    <td align="right">T.V:</td>
    <td><input type="text" name="tv" size="30"></td>
  </tr>
  <tr>
    <td align="right">Books</td>
    <td><input type="text" name="books" size="30"></td>
  </tr>
  <tr>
    <td align="right">Heroes:</td>
    <td><input type="text" name="heroes" size="30"></td>
  </tr>
  <tr>
    <td><input type= 'hidden' name= 'commented' value= 'set' ></td> 
    <td><input type='submit' name='submit' value='Submit'></td>
  </tr>
  </form>
</table>

<?php
}
else{
// form submitted
// set server access variables
$host = "*********";
$user = "*********";
$pass = "*********";
$db = "***********";

$name = $_POST['name'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$headline = $_POST['headline'];
$aboutme = $_POST['aboutme'];
$meet = $_POST['meet'];
$music = $_POST['music'];
$films = $_POST['films'];
$tv = $_POST['tv'];
$books = $_POST['books'];
$heroes = $_POST['heroes'];



    // open connection
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($db) or die ("Unable to select database!");
    
    // create query
    $query = "INSERT INTO members_profile (name, surname, username, headline, aboutme, meet, music, films, tv, books, heroes) VALUES ('$name', '$surname', '$username', '$headline', '$aboutme', '$meet', '$music', '$films', '$tv', '$books', '$heroes')";
    
    // execute query
    $result = mysql_query($query)or die("Error in query: $query. ".mysql_error()); 
    
    // print message with ID of inserted record
    echo "New record inserted with ID ".mysql_insert_id();
    echo "<br><br><a href='#' onClick='history.go(-1)'>Back</a>";
    
    // close connection
    mysql_close($connection);
}
    
    
?>

</body>
</html>
Thanks
Alex


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: SOLVED

Post by feyd »

acwe88 wrote:Apologies feyd but i have'nt got time to add the code properly
The button to add

Code: Select all

is the button left of

Code: Select all

. If you don't have the time to post correctly, hold off on posting until you do have time.
Post Reply