Jump to content

[SOLVED] inserting data into MySQL


black_box

Recommended Posts

I am having a problem inserting data into MySQL what can be the problem ??

 

$SQL_insert = "INSERT into borrow VALUES ('','$bok_id','$bok_nam','$stu_id','$stu_nam','$DOI','$due_dat','','')";
mysql_query($SQL_insert)or die("Couldnot insert");

 

Database structure:

 

The first column is auto increament and last two columns can be null.

 

Date is also in : m/d/y format so MySQL should accept tht as it is.

 

So what can be the problem please help me i m stuck for 4 hrs :(

Link to comment
https://forums.phpfreaks.com/topic/79313-solved-inserting-data-into-mysql/
Share on other sites

try removing the quotes around the id's , if im not mistaken mysql will treat '1' as a string and not an integer.

so something like this would be my guess.

<?php
$SQL_insert = "INSERT into borrow VALUES ('',$bok_id,'$bok_nam',$stu_id,'$stu_nam','$DOI','$due_dat','','')";
?>

 

hope this helps

Try

 

$SQL_insert = mysql_query(sprintf("INSERT INTO `borrow` ( `table_name`, `table_name`, `table_name`, `table_name`, `table_name`, `table_name`, `table_name`, `table_name`, `table_name` ) 
VALUES ( '', '%d', '%s', '%d', '%s', '%s', '', '', '')", $bok_id, $bok_name, $stu_id, $stu_nam, $DOI, $due_date)) 
or die('Error: ' . mysql_error());

 

The table_name's are for you to enter the table names for each table where you want to insert the data.

 

e.g.

 

`bok_name` for the variable $bok_name // `bok_name` being the table name, and $bok_name being the variable.

 

I'm assuming that $bok_id and $stu_id are integer's therefore i've used %d for them rather than %s for strings.

 

Alternitavely

 

$SQL_insert = mysql_query(sprintf("INSERT INTO `borrow` ( `table_name`, `table_name`, `table_name`, `table_name`, `table_name` ) 
VALUES ( '%d', '%s', '%d', '%s', '%s' )", $bok_id, $bok_name, $stu_id, $stu_nam, $DOI, $due_date)) 
or die('Error: ' . mysql_error());

 

I have removed the all of the ('')'s which contain no data

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.