Jump to content

Archived

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

bunker

Form and POST problem

Recommended Posts

Hi!

Just need some quick help please.. My script is very simple.. fichas.php has a form just to add a record to th Database with the data inserted in two fields.. but for some reason when the POST is used I dont get the values... The new record is created in the data base, but because I also pass a variable using _GET.

The code is

$ele_tec = $_POST['new_ele'];
$valor = $_POST['new_valor'];
$id_potencia = $_GET['id_potencia'];
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('$ele_tec', '$valor','$id_potencia')");

And the form;

echo "<form name=\"agregar\" action='" .$_SERVER['SELF'] ."' method='post'>";
echo "<br><input type=\"text\" name=\"new_ele\">";
echo "<input type=\"text\" name=\"new_valor\">";
echo "<input type=\"hidden\" name=\"id_potencia\" value=\"$row[id_potencia]\">";
echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";
echo "</form>";

There is some code, before, between and after.. But I think that the problem is in the code above.

Also.. how do I quote code in the forum??

Thx in advanced!!

Share this post


Link to post
Share on other sites
try doing it like this
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('[b]{$ele_tec}[/b]', '[b]{$valor}[/b]','[b]{$id_potencia}[/b]')");


note the curly braces

Share this post


Link to post
Share on other sites
[!--quoteo(post=362579:date=Apr 7 2006, 11:37 AM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Apr 7 2006, 11:37 AM) [snapback]362579[/snapback][/div][div class=\'quotemain\'][!--quotec--]
try doing it like this
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('[b]{$ele_tec}[/b]', '[b]{$valor}[/b]','[b]{$id_potencia}[/b]')");
note the curly braces
[/quote]

Nop... Same result!

Ive decided to post all the code...

<?php include("../include/config.php");
$connection = mysql_connect("$dbhost", "$dblogin", "$dbpass");
$db = mysql_select_db("$dbname", $connection);
$sql = mysql_query("SELECT p.id AS pid, p.desc AS pdesc FROM potencia p");

$id = $_GET['id'];
$action = $_GET['action'];

switch ($action){
case "delete":
delete();
break;

case "agregar":
agregar();
break;
}

function delete() {
$id_elemento = $_GET['id_elemento'];
$sql = mysql_query("DELETE FROM elemento USING elemento WHERE elemento.id = '$id_elemento'");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
header ("Location: fichas.php?id=1");
}

function agregar() {
$ele_tec = $_POST['new_ele'];
$valor = $_POST['new_valor'];
$id_potencia = $_GET['id_potencia'];
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('{$ele_tec}', '{$valor}','{$id_potencia}')");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
echo $ele_tec;
//header ("Location: fichas.php?id=1");
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
?>
<p>
<select name="potencia[]" id="potencia" onchange="location = this.options[this.selectedIndex].value;">
<option>- Seleccione -</option>
<?php
while ($row = mysql_fetch_array($sql)) {
echo "<option value=\"fichas.php?id=$row[pid]\">$row[pdesc]</option>";
}
?>

</select>
<?php
if (!empty($id)){

$sql = mysql_query("SELECT * FROM elemento WHERE id_potencia = $id");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }

while ($row = mysql_fetch_array($sql)) {
echo "<br><input name=\"ele_tec\" type=\"text\" id=\"ele_tec\" value=\"$row[ele_tec]\">";
echo "<input name=\"valor\" type=\"text\" id=\"valor\" value=\"$row[valor]\">";
echo "<input name=\"editar\" type=\"button\" id=\"editar\" value=\"Editar\">";
echo "<input name=\"eliminar\" type=\"button\" id=\"eliminar\" value=\"Eliminar\" onclick=\"location.href='fichas.php?action=delete&id_elemento=$row[id]'\">";
}
$sql = mysql_query("SELECT id_potencia FROM elemento WHERE id_potencia = $id");
$row = mysql_fetch_array($sql);
echo "<form name=\"agregar\" action='" .$_SERVER['SELF'] ."' method='post'>";
echo "<br><input type=\"text\" name=\"new_ele\">";
echo "<input type=\"text\" name=\"new_valor\">";
echo "<input type=\"hidden\" name=\"id_potencia\" value=\"$row[id_potencia]\">";
echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";
echo "</form>";
}
?>
</p>

</body>
</html>


Thx once again!

Share this post


Link to post
Share on other sites
At the start of the script place the following debugging code:
[code]<?php
if (isset($_POST['anadir'])) {
    echo '<pre> ---- $_GET ---- ' . print_r($_GET,true) . '</pre>';
    echo '<pre> ---- $_POST ---- ' . print_r($_POST,true) . '</pre>';
}?>[/code] to see what is being passed back to your script.

In your function, put the query into a separate string and echo it to see what the query looks like.

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=362588:date=Apr 7 2006, 12:08 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 7 2006, 12:08 PM) [snapback]362588[/snapback][/div][div class=\'quotemain\'][!--quotec--]
At the start of the script place the following debugging code:
[code]<?php
if (isset($_POST['anadir'])) {
    echo '<pre> ---- $_GET ---- ' . print_r($_GET,true) . '</pre>';
    echo '<pre> ---- $_POST ---- ' . print_r($_POST,true) . '</pre>';
}?>[/code] to see what is being passed back to your script.

In your function, put the query into a separate string and echo it to see what the query looks like.

Ken
[/quote]

Done.. And the result is a NOTHING... When echoing the Sql query I just get a simple 1 ... because on the GET variable.

Could it be something to do with the form? Ive checked it several times but I see nothing wrong!

Bye

Share this post


Link to post
Share on other sites
You cannot do this:
[code] echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";[/code]
As the submit button will over ride the javascript (location.href) command when submitting your form.

If you want to pass GET data then you'll need to place that in the action attribute in the form tag. IE:
[code]echo "<form action=" . $_SERVER['PHP_SELF'] . "?action=agregar&id_potencia=" . $row[id_potencia] . ">";[/code]
But you cannot use the location.href command when submitting the form as the form will override this.

Share this post


Link to post
Share on other sites

×

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.