Jump to content

can some one explain this code line by line


gibigbig

Recommended Posts

<?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";mysql_close($con)

?>

 

Link to comment
Share on other sites

<?php
// connect to database localhost with username peter and password abc123
$con = mysql_connect("localhost","peter","abc123");

// if the connection did not take do inside the if.
if (!$con)
  {
  // kill the script as a db connection could not be made
  die('Could not connect: ' . mysql_error());
  }
// select the database (my_db) we wish to use with our db connection
mysql_select_db("my_db", $con);
// insertt data into the database using POST data from a form.
$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

// if the query returns false for $sql than die and print out the error
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

// print that everything went good.
echo "1 record added";
// close the connection.
mysql_close($con)
?> 

Link to comment
Share on other sites

The table is selected in the query ($sql) - "person" would be your table.

 

<?php
//INSERT INTO [tableName]=person 

$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
?>

Link to comment
Share on other sites

Look at this line right here:

 

<?php

$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

?>

 

It does specify a table.

 

INSERT INTO person

 

"person" is the name of the table that you are storing the information in.

 

EDIT: Geez your fast Frost, haha.

Link to comment
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.