Jump to content

problems with the INSERT INTO command for MySQL

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
LCD344

LCD344

    Newbie

  • Members
  • Pip
  • 7 posts
hii, i have started working with php and MySQL last week, and i am trying to build myself a HTML interface that i can use MySQL through... problem is, i cant seem to be able to use the INSERT INTO command... the query is executed, but no changes are made in the table itself... other queries like show and describe are working... im using latest PHP version (5.3.0 i think) and MySQL 5.1

here is my code

<?php

include_once 'nur_MySQL_connect.php';


if ($_POST)

{

	print_r($_POST);


	$table = ($_POST['table']);

	$host = ($_POST['host']);

	$user = ($_POST['user']);

	$pass = ($_POST['pass']);

	$database = ($_POST['db']);

	$action= ($_POST['act']);

} 


$mysqli = nur_MySQL_connect($host,$user,$pass,$database);



if ($action == "add2table")

{

                $line= ($_POST['newLine']);


	$quer = "INSERT INTO $table VALUES $line";

	

//	$quer = $mysqli->real_escape_string($quer); 

//             i tried using the escape sctring... but then the query doesnt work

	$mysqli->query($quer) or die ("$quer");

	echo  $mysqli->affected_rows;

}


?>


basicly it prints 1 (which is for the $mysqli->affected_rows) but it doesnt update the table...

thank you very much

LCD

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If you are inserting values into columns, then your query must look like this:
$quer = "INSERT INTO $table (column1) VALUES ($line)";
You would need to replace `column1` with the column you are wishing to place the value into, otherwise you are inserting the data into arbitrary columns.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
LCD344

LCD344

    Newbie

  • Members
  • Pip
  • 7 posts
i tried that before... and it also didnt work. also from the MySQL manual it says i dont have to specify the column names...

MySQL :: MySQL 5.1 Reference Manual :: 3.3.3 Loading Data into a Table (about the insert... on the last lines of the page in the manual)

[edit]
apearntly the same thing exactly works if i use old school php mysql extention instead of mysqli... i changed the last part into

if ($action == "add2table")

{

	$line= ($_POST['newLine']);

	$link = mysql_connect($host,$user,$pass);

	mysql_select_db($database, $link);


	$query = "INSERT INTO $table VALUES $line";

	$result = mysql_query($query);

	

//	WTF > apearently MYSQLI wont do INSERT ?	

//	$quer = $mysqli->real_escape_string($quer);

//	$mysqli->query($quer) or die ("$quer");

//	nur_MySQL_command2Array($mysqli,$quer);

}

and the insert works now... anyone knows if its a known problem with mysqli ?

thanks

LCD

Edited by LCD344, 12 October 2010 - 04:15 PM.
solved ???


#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Well, you don't have to specify columns, if you have values for all columns and in the correct order. if you have values in another order, or for less than all columns, you need to specify columns.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
LCD344

LCD344

    Newbie

  • Members
  • Pip
  • 7 posts
got it... thank you, what realy bothers me is why mysqli doesnt work and mysql commands do though