Adding a record in a table??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
phpnewbie1985
Forum Commoner
Posts: 33
Joined: Thu Jan 08, 2004 6:15 am

Adding a record in a table??

Post by phpnewbie1985 »

Is there a basic script that i can use to add a new record to one of my databases? I have successfuly managed to make a database, table and added a record, but i would like to make a new record from one of my pages and click a button to add the record... I have looked around and tried to understand some of the scripts going round but none of them seem to be working :S

Can anyone help me out with this problem that im having?
many thanks
Chris
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You want to know how to insert data into a database using PHP ?
If this is the case then what type of database are you using ?
phpnewbie1985
Forum Commoner
Posts: 33
Joined: Thu Jan 08, 2004 6:15 am

Post by phpnewbie1985 »

Im using MySQL and also using PHP MyAdmin but i dont want to use PHP MyAdmin to add records....
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

I used this and it seemed to work:

$result = mysql_query("INSERT INTO tblname( field1, Field2, field3, field4, field5) VALUES( '$field1', '$field2', '$field3', '$field4', '$field5')", $linkID);
if ($result == true)
{
print "Record added<p>";
}
else
{
print "Something wrong here<p>";
}

Insert you field names where i have put field1 etc

Hope this helps
phpnewbie1985
Forum Commoner
Posts: 33
Joined: Thu Jan 08, 2004 6:15 am

Post by phpnewbie1985 »

I hope i dont sound dumb saying this, but what do i tell the form action to do? i never thought learning php could be so annoying :S lol

Thanks
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

1-) Form will post to datas to the page itself.
2-) At the top of the file, there will be an if-statement.
3-) If(is_data_posted)
4-) If statement is false, then show from.
5-) If it is true then run insert query.

I think http://www.w2schools.com will be very helpfull for you.
Good Luck.
phpnewbie1985
Forum Commoner
Posts: 33
Joined: Thu Jan 08, 2004 6:15 am

Post by phpnewbie1985 »

Can you see any problems with this code? it doesnt say anything after pressing the submit button it goes back to the page with clear fields? It all looks ok to me? :S :lol:

Code: Select all

<?php

//If the submit button is pressed
if($action == add){ 

//Connect To Database
require_once ('../mysql_connect.php'); 

//Add the new record
$result = mysql_query("INSERT INTO nokia6600( position, name, date, order_number) VALUES( '$position', '$name', '$ordernumber', '$date')"); 
if ($result == true) 
{ 
print "Record added<p>"; 
} 
else 
{ 
print "Something wrong here<p>"; 
} 
} 
?>

<form name="Add_Record" action="add.php?=add" method="post">
  <table width="95%" border="0" align="center">
    <tr> 
      <td><div align="right">Name :</div></td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr> 
      <td><div align="right">Position :</div></td>
      <td><input name="position" type="text" maxlength="2"></td>
    </tr>
    <tr> 
      <td><div align="right">Date :</div></td>
      <td><input type="text" name="date"></td>
    </tr>
    <tr>
      <td><div align="right">Order Number : </div></td>
      <td><input type="text" name="ordernumber"></td>
    </tr>
    <tr> 
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
  <p align="center">&nbsp;</p>
</form>
?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

dismiss this post....
Last edited by dethron on Fri Feb 13, 2004 6:54 am, edited 1 time in total.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

And also if you still want to use,

Code: Select all

<?php
if($action == add){ 
?>
Then put this into form

Code: Select all

&lt;input name="action" value="add" type="hidden"&gt;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

buyukcer - PHP doesn't have access to the name of the form so the code you posted can't work.

phpnewbie1985 - you need to start using the $_POST and $_GET arrays to access information sent via the POST method and GET method (ie. in the URL) respectively. So instead of:

Code: Select all

<?php

//If the submit button is pressed
if($action == add){

//Connect To Database
require_once ('../mysql_connect.php');

//Add the new record
$result = mysql_query("INSERT INTO nokia6600( position, name, date, order_number) VALUES( '$position', '$name', '$ordernumber', '$date')");
if ($result == true)
{
print "Record added<p>";
}
else
{
print "Something wrong here<p>";
}
}
?>
<form name="Add_Record" action="add.php?=add" method="post">
  <table width="95%" border="0" align="center">
    <tr>
      <td><div align="right">Name :</div></td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr>
      <td><div align="right">Position :</div></td>
      <td><input name="position" type="text" maxlength="2"></td>
    </tr>
    <tr>
      <td><div align="right">Date :</div></td>
      <td><input type="text" name="date"></td>
    </tr>
    <tr>
      <td><div align="right">Order Number : </div></td>
      <td><input type="text" name="ordernumber"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
  <p align="center"> </p>
</form>
you should try:

Code: Select all

<?php

// make sure you can see any error messages:
ini_set('display_errors', 1);
error_reporting(E_ALL);

// check whether the action variable exists in the URL and then
// whether it is equal to 'add'
// always put strings in quotes, you must have 'add' not add
if (isset($_GET['action']) && $_GET['action'] == 'add') {

	// require, include etc. don't need parenthesis as they are not
	// functions as such
	require_once '../mysql_connect.php';

	// you need to access the posted data via the $_POST array, lets
	// get rid of leading and trailing spaces and assign each variable
	// to the name we need to access it later:
	foreach ($_POST as $key => $value) {
		$$key = trim($value);
	}

	// you should really be doing some validation before you put user
	// inputted data directly into the database - think about making
	// sure that numbers are numbers, dates are dates and that strings
	// don't contain characters that could be dangerous (SQL injection)
	// you should also make sure that any required information exists

	// separate out your SQL query so you can debug easier
	$sql    = "INSERT INTO nokia6600( position, name, date, order_number) VALUES( '$position', '$name', '$ordernumber', '$date')";

	// add error handling - code will stop executing if there is a
	// problem with the database query
	$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
	
	echo 'Record was added';
} else {
	echo 'The form has not been submitted';
}

?> 

<!-- Note that you need to put action=add -->
<form name="Add_Record" action="add.php?action=add" method="post">
  <table width="95%" border="0" align="center">
    <tr>
      <td><div align="right">Name :</div></td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr>
      <td><div align="right">Position :</div></td>
      <td><input name="position" type="text" maxlength="2"></td>
    </tr>
    <tr>
      <td><div align="right">Date :</div></td>
      <td><input type="text" name="date"></td>
    </tr>
    <tr>
      <td><div align="right">Order Number : </div></td>
      <td><input type="text" name="ordernumber"></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
  <p align="center"> </p>
</form>
Mac
Post Reply