/    Sign up×
Community /Pin to ProfileBookmark

Doesnt Insert Value

can someone help me what is wrong with this

[COLOR=DarkRed]INSERT INTO clientes VALUES(‘$_POST[id]’,’$_POST[doc_id]’,’$_POST[nombre]’,’$_POST[direccion_fact]’)[/COLOR]

it doesnt insert the values

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@sitehatcheryAug 15.2006 — for one, no closing semi-colon.
Copy linkTweet thisAlerts:
@r4gn0authorAug 15.2006 — WHAT?

explain yourself please
Copy linkTweet thisAlerts:
@sitehatcheryAug 15.2006 — You have to have a semi-colon at the end
Copy linkTweet thisAlerts:
@r4gn0authorAug 15.2006 — the ;

thats what youre talking?
Copy linkTweet thisAlerts:
@sitehatcheryAug 15.2006 — yep.

How are you using that query?
Copy linkTweet thisAlerts:
@r4gn0authorAug 15.2006 — require_once('Connections/principal.php');

$miconexion = new Db("webappdetec","localhost","webappdetec","webappdetecpass") ;

$miconexion->consulta("INSERT INTO clientes VALUES ('$_POST['id']','$_POST['doc_id']','$_POST['nombre']','$_POST['direccion_fact']')");

principal.php is where the class DB is and it also has the function to connect the db and controls the query
Copy linkTweet thisAlerts:
@sitehatcheryAug 15.2006 — Did you get that working?
Copy linkTweet thisAlerts:
@balloonbuffoonAug 15.2006 — Are any of those columns integer type?
Copy linkTweet thisAlerts:
@sitehatcheryAug 15.2006 — Also, make sure you are inserting the right number and order of values into the database. You may want to echo back the error message.
Copy linkTweet thisAlerts:
@r4gn0authorAug 15.2006 — the values are in the same order of the insertion and the id and doc_id value are integer
Copy linkTweet thisAlerts:
@balloonbuffoonAug 15.2006 — You should not use any quotes around values that are supposed to be integer. Additionally, you can't use array values directly inside of double quotes if the key is a string, you must instead break apart the query and append the array values where they belong, so your query should look like this:[code=php]
"INSERT INTO clientes VALUES (".$_POST['id'].",".$_POST['doc_id'].",'".$_POST['nombre']."','".$_POST['direccion_fact']."')"[/code]
See if that works for you.

--Steve
Copy linkTweet thisAlerts:
@balloonbuffoonAug 16.2006 — Let me just show you why it is necessary to break up the string like that; here are some examples that show how to construct strings using variables:
[code=php]
//you can include variables directly in double quoted strings, and the value will be added to the string
$foo = "bar";
echo "The variable foo is $foo"; //The variable foo is bar

//you can include array values directly in double quoted strings if the key is not a string
$blah = array("hey","foo"=>"bar","ho hum"=>"bored");
echo "The first value in the array is $blah[0]"; //The first value in the array is hey
//you CANNOT include array values directly in double quoted strings if the key is a string
echo "The second value in the array is $blah['foo']"; //parse error!
//technically you can include an array value without using single quotes around the key even if its a string, and it will work
echo "The second value in the array is $blah[foo]"; //The second value in the array is bar
//this will not work, though, with array keys that contain spaces
echo "The third value in the array is $blah[ho hum]"; //parse error!

//you can break up a string to stick in any values; the pieces must be put back together with periods (.)
echo "The third value in the array is ".$blah['ho hum']; //The third value in the array is bored

//there are yet more methods for including values in strings
// you can use printf()
printf("The third value in the array is %s", $blah['ho hum']); //The third value in the array is bored
//check the manual for more information about this function
[/code]

I hope you got a better understanding about this!

--Steve
Copy linkTweet thisAlerts:
@balloonbuffoonAug 16.2006 — Here's how you would do it with sprintf():
[code=php]$miconexion->consulta(sprintf("INSERT INTO clientes VALUES (%d,%d,'%s','%s')", $_POST['id'], $_POST['doc_id'], $_POST['nombre'], $_POST['direccion_fact']));
[/code]
Its a nice function and makes things easier to read in a lot of cases. BTW-- printf() is equivalent to echo'ing sprintf(). Sorry about getting carried away with telling you all this stuff. :rolleyes:

--Steve
Copy linkTweet thisAlerts:
@r4gn0authorAug 16.2006 — id didnt work
Copy linkTweet thisAlerts:
@balloonbuffoonAug 16.2006 — You need to be a smart debugger. Echo the query that you're using and make sure it is what you intend it to be. Echo the mysql error using [url=php.net/mysql_error]mysql_error()[/url]. Did you make your database class yourself, or get it elsewhere? Are you sure it works? Are you getting any php errors? There's only so much that other people can do to help you debug, try/look for these things and report back.

--Steve
Copy linkTweet thisAlerts:
@r4gn0authorAug 16.2006 — HERE IS THE COMPLETE page...it doesnt works i tried introducing static values but still doesnt works...im new with this...is the rest of the page ok? is the button sending to where its suposed?

PLEASE I NEED HELP!

<body>

<?php

if (isset($_POST["submit"])) {

require_once('Connections/principal.php');

$miconexion = new Db("webappdetec","localhost","webappdetec","webappdetecpass") ;

$miconexion->consulta("INSERT INTO clientes (id, doc_id, nombre, direccion_fact) VALUES (".$_
POST['id'].",".$_POST['doc_id'].",'".$_POST['nombre']."','".$_POST['direccion_fact']."')");

}

?>

<form method="post" name="form1" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">

<p align="center" class="Estilo9">AGREGAR CLIENTES </p>

<table align="center">

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Id:</span></div></td>

<td><input name="id" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Documento Id:</span></div></td>

<td><input name="doc_id" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Nombre:</span></div></td>

<td><input name="nombre" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Direccion Facturacion:</span></div></td>

<td><input name="direccion_fact" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><a href="clientes.php"><img src="Atras.jpg" width="59" height="23" border="0" /></a></td>

<td><div align="right">

<input type="submit" name "submit" value="Agregar">

</div></td>

</tr>

</table>

</form>

<p>&nbsp;</p>

</body>
Copy linkTweet thisAlerts:
@r4gn0authorAug 16.2006 — I Did It!!! It Worked!!

<body>

<?php

require_once('Connections/principal.php');

$miconexion = new Db("webappdetec","localhost","webappdetec","webappdetecpass") ;

$miconexion->consulta("INSERT INTO clientes (id, doc_id, nombre, direccion_fact) VALUES (".$_POST['id'].",".$_POST['doc_id'].",'".$_POST['nombre']."','".$_POST['direccion_fact']."')");

?>

<form method="post" name="form1" action="agregarc.php">

<p align="center" class="Estilo9">AGREGAR CLIENTES </p>

<table align="center">

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Id:</span></div></td>

<td><input name="id" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Documento Id:</span></div></td>

<td><input name="doc_id" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Nombre:</span></div></td>

<td><input name="nombre" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><div align="left" class="Estilo8"><span class="Estilo7">Direccion Facturacion:</span></div></td>

<td><input name="direccion_fact" type="text" value="" size="32"></td>

</tr>

<tr valign="baseline">

<td nowrap align="right"><a href="clientes.php"><img src="Atras.jpg" width="59" height="23" border="0" /></a></td>

<td><div align="right">

<input type="submit" name "submit" value="Agregar">

</div></td>

</tr>

</table>

</form>

<p>&nbsp;</p>

</body>


but now it shows


Notice: Undefined index: id in /var/www/html/appdetec/agregarc.php on line 24

Notice: Undefined index: doc_id in /var/www/html/appdetec/agregarc.php on line 24

Notice: Undefined index: nombre in /var/www/html/appdetec/agregarc.php on line 24

Notice: Undefined index: direccion_fact in /var/www/html/appdetec/agregarc.php on line 24


when im going to input a value for the first time
Copy linkTweet thisAlerts:
@balloonbuffoonAug 16.2006 — You should only be running the database query if something has been submitted via form:
[code=php]<?php
if (count($_POST)) {
require_once('Connections/principal.php');
$miconexion = new Db("webappdetec","localhost","webappdetec","webappdetecpass") ;
$miconexion->consulta("INSERT INTO clientes (id, doc_id, nombre, direccion_fact) VALUES (".$_POST['id'].",".$_POST['doc_id'].",'".$_POST['nombre']."','".$_POST['direccion_fact']."')");
}
?>[/code]
Copy linkTweet thisAlerts:
@r4gn0authorAug 16.2006 — it doesnt show the notices, but only make one insertion, when i go to the insert page again doesnt make anything
×

Success!

Help @r4gn0 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...