how to insert

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
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

how to insert

Post by hrubos »

hi,
Plz, show me how to do if I want to insert id_reservation into database when user clik to radiobutton.In my dtabase id_resrevation is primary key and auto_increment

I have written but not run

Code: Select all

//class interface
<html>
<body>
<form method = "POST" action = "data.php">
<p>
<input type="radio"  name="id_reservation" />Rezervace</p>
<p>
<input type="submit" value="Submit" name="submit"> </p>
</body>
</html>

//class to insert into database

<html>
<body>
<?php
$id_reservation = $_POST['id_reservation'];

 if(!get_magic_quotes_gpc())
 {
 $id_reservation  = addslashes($id_reservation);
 }
 @ $db = mysql_connect("localhost", "root","ha1985") or die("Error");
 mysql_select_db("kolej",$db) or die("Can't choose database");
 $query = "insert into reservation " . "(id_reservation) values"
			. "('$id_reservation')";
				mysql_query($query);
 
?>
</body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your radio button has no value. What's the end goal of this code?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

feyd wrote:Your radio button has no value. What's the end goal of this code?
I tried :

Code: Select all

<input type="radio"  value = "1" name="id_reservation">Rezervace</p>
but in database nothing has been inserted?wherenever is still mistake???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried echoing $query to see what query you're sending MySQL? Have you added debugging to your queries such as

Code: Select all

mysql_query($foo) or die(mysql_error());
These are all very basic debugging steps that everyone, not just yourself, should know and use to help us help you.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

feyd wrote:Have you tried echoing $query to see what query you're sending MySQL? Have you added debugging to your queries such as

Code: Select all

mysql_query($foo) or die(mysql_error());
These are all very basic debugging steps that everyone, not just yourself, should know and use to help us help you.
thank, this function runs well.

In database there are table Reservation,which contains id_reservation,id_student.But when I inserted id_reservation by cliking radiobutton,I have error : "Field 'id_student' doesn't have a default value" ???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That means you need to pass additional information so that id_student has a value associated. Since there is no default value you must set that field with every insertion.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

okei, I understand how to do, thanxxx much.
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

hej, but I can do this with table,which has 1 rows, more attributes have error : Field '...' doesn't have a default value

means that I have to insert all attributes in tables. So how to do if I want to insert only attribut in table???
User avatar
megamoose
Forum Newbie
Posts: 5
Joined: Fri Nov 24, 2006 3:21 am

Post by megamoose »

hrubos wrote:hej, but I can do this with table,which has 1 rows, more attributes have error : Field '...' doesn't have a default value

means that I have to insert all attributes in tables. So how to do if I want to insert only attribut in table???
I'm not sure but I think the reason it's tellign you this is maybe the field that doesn't have a default value is a required field, and therefore if you don't tell it what value you want to add to the database, it will look to find a default value, then not find one and fail to insert the row.

That might just be your problem. I know that generally you do not have to enter all data in fields only the ones you want.

Mark
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

sounds like you are using MySQL 5 with strict mode on. In this case, you have to enter something into each field.
Post Reply