Adding a record in a table??
Moderator: General Moderators
-
phpnewbie1985
- Forum Commoner
- Posts: 33
- Joined: Thu Jan 08, 2004 6:15 am
Adding a record in a table??
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
Can anyone help me out with this problem that im having?
many thanks
Chris
-
phpnewbie1985
- Forum Commoner
- Posts: 33
- Joined: Thu Jan 08, 2004 6:15 am
-
nutstretch
- Forum Contributor
- Posts: 104
- Joined: Sun Jan 11, 2004 11:46 am
- Location: Leicester
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
$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
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.
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
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
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>
?>And also if you still want to use,
Then put this into form
Code: Select all
<?php
if($action == add){
?>Code: Select all
<input name="action" value="add" type="hidden">- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
you should try:
Mac
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>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>