Page 1 of 1

how to insert

Posted: Sun Nov 26, 2006 4:05 am
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>

Posted: Sun Nov 26, 2006 7:38 am
by feyd
Your radio button has no value. What's the end goal of this code?

Posted: Sun Nov 26, 2006 8:53 am
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???

Posted: Sun Nov 26, 2006 9:02 am
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.

Posted: Sun Nov 26, 2006 9:27 am
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" ???

Posted: Sun Nov 26, 2006 9:31 am
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.

Posted: Sun Nov 26, 2006 9:38 am
by hrubos
okei, I understand how to do, thanxxx much.

Posted: Sun Nov 26, 2006 10:04 am
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???

Posted: Sun Nov 26, 2006 3:04 pm
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

Posted: Sun Nov 26, 2006 4:10 pm
by tsg
sounds like you are using MySQL 5 with strict mode on. In this case, you have to enter something into each field.