...

View Full Version : Edit a Database with PHP



Namii
01-14-2010, 12:11 PM
Hi guys. I need some help in the edit part of the PHP.

I've created a form and tried to fetch or request the data from the database and i still cant do it.

In the HTML i tried:

<p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>

And in the PHP:

$update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";

$result = mysql_query($update, $connection);

And how do i navigate in the page to edit the other ones.

Thanks a lot and sorry for bothering.. I'm still a PHP rookie.

JAY6390
01-14-2010, 12:16 PM
You do understand that PHP is a server side language and that it runs before html gets output, so doing stuff on the html page won't do anything unless you post/get your data to a script. If you want some help with this you will need to post your whole code

Namii
01-14-2010, 12:38 PM
My PHP code is the following:

<?php

$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$morada = $_POST['morada'];
$pontos= $_POST['pontos'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="edit_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";

$result = mysql_query($update, $connection);

if (($result)==1){
echo "<p>Dados alterados<br>";
}

?>

And the HTML code of the form is:

<form method = "POST" action = "edit_fornec.php">
<fieldset>
<p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
<p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>
Nome:
<input type = "text" name = "nome">
</p>
<p align="center" class="style1">Telefone:
<input type = "text" name = "telefone">
</p>
<p align="center" class="style1">E-Mail:
<input type = "text" name = "e_mail">
</p>
<p align="center" class="style1">Empresa:
<input type = "text" name = "empresa">
</p>
<p align="center" class="style1">
<input type = "submit" value = "Alterar">
</fieldset>
</form>

masterofollies
01-14-2010, 07:15 PM
I don't see a POST submission area on your code. Try this.


<?php

if ($_POST['submit'])
{

$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$morada = $_POST['morada'];
$pontos= $_POST['pontos'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="edit_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id']."'";

$result = mysql_query($update, $connection);

if (($result)==1){
echo "<p>Dados alterados<br>";
}
}
else {
?>

And the HTML code of the form is:

<form method = "POST" action = "edit_fornec.php">
<fieldset>
<p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
<p align="center" class="style1">ID: <?php echo $_REQUEST['id_fornec'];?><br/>
Nome:
<input type = "text" name = "nome">
</p>
<p align="center" class="style1">Telefone:
<input type = "text" name = "telefone">
</p>
<p align="center" class="style1">E-Mail:
<input type = "text" name = "e_mail">
</p>
<p align="center" class="style1">Empresa:
<input type = "text" name = "empresa">
</p>
<p align="center" class="style1">
<input type = "submit" name="submit" value = "Alterar">
</fieldset>
</form>
<?php } ?>

Namii
01-14-2010, 07:59 PM
Ok i tried it but now it gives me an error in line 23.. Which is:

Parse error: parse error in C:\wamp\www\editar_fornec.php on line 23

and line 23 is

$update = "UPDATE fornecedores set nome = '".$_REQUEST ['nome']."',telefone = '".$_REQUEST['telefone']."',e_mail = '".$_REQUEST['e_mail']."',empresa = '".$_REQUEST['empresa'] where id = .$_REQUEST['id_fornec']."'";

I don't see anything wrong there, maybe because i'm not very familiar with PHP yet don't know..

shadowmaniac
01-14-2010, 08:27 PM
Use
and [/php ] to enclose your php code on the forums...
You messed up the string concatenation with respect to the last 2 variables and I'm not sure if "$_REQUEST ['nome']" with the `extra` space is valid either.
One more thing: you're already assigning the $_POST values to some variables so, no need to use the $_REQUEST superglobal.
[php]
$update = "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone'."',e_mail = '".$e_mail."',empresa = '".$_POST['empresa']."' where id = '".$_POST['id_fornec']."'";

I'm only using $_POST for the last 2 variables as you haven't assigned them to anything as opposed to the previous variables.

Namii
01-14-2010, 09:47 PM
Ok i tried that too but now i'm getting Notices about undefined index of all the variables i'm trying to use.. Even submit ( Notice: Undefined index: submit in C:\wamp\www\editar_fornec.php on line 3 )

At the moment my code is the following




<?php

if ($_POST['submit'])
{

$id_fornec = $_POST['id_fornec'];
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$empresa = $_POST['empresa'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="editar_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$update = "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone."',e_mail = '".$e_mail."',empresa = '".$_POST[$empresa]."' where id = '".$_POST['id_fornec']."'";

$result = mysql_query($update, $connection);

if (($result)==1){
echo "<p>Dados alterados<br>";
}
}
else {

?>

<form method = "POST" action = "editar_fornec.php">
<fieldset>
<p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
<p align="center" class="style1">ID: <?php echo $_POST['id_fornec'];?><br/>
Nome: <?php echo $_POST['nome'];?>
<input type = "text" name = "nome">
</p>
<p align="center" class="style1">Telefone: <?php echo $_POST['telefone'];?>
<input type = "text" name = "telefone">
</p>
<p align="center" class="style1">E-Mail: <?php echo $_POST['e_mail'];?>
<input type = "text" name = "e_mail">
</p>
<p align="center" class="style1">Empresa: <?php echo $_POST['empresa'];?>
<input type = "text" name = "empresa">
</p>
<p align="center" class="style1">
<input type = "submit" name="submit" value = "Alterar">
</fieldset>
</form>
<?php } ?>



Notice: Undefined index: id_fornec in C:\wamp\www\editar_fornec.php on line 57
Notice: Undefined index: nome in C:\wamp\www\editar_fornec.php on line 58
Notice: Undefined index: telefone in C:\wamp\www\editar_fornec.php on line 61
Notice: Undefined index: e_mail in C:\wamp\www\editar_fornec.php on line 64
Notice: Undefined index: empresa in C:\wamp\www\editar_fornec.php on line 67

Thank you for your patience ( and i didn't knew about the php tags to put the code sorry)

Rebbu
01-14-2010, 11:16 PM
You need to test if it isset first.




if (isset($_POST['submit']) && $_POST['submit'])

Namii
01-14-2010, 11:55 PM
I tried it the line 3 notice is gone thank you but the other ones still pervade

shadowmaniac
01-15-2010, 01:35 AM
Same deal as pointed out by Rebbu. In your HTML code section, wherever you have something like


<?php echo $_POST['id_fornec'];?>

change it to


<?php if(isset($_POST['id_fornec'])) { echo $_POST['id_fornec']; } ?>


As per your provided code and as reported by php, 5 such changes have to be made.

Reason it's not working is because the first time you access the page, you have not yet submitted any data yet so variables like $_POST['id_fornec'] are basically non-existent hence the php warnings and the reason why you need to check whether they've been set first using the isset() function before trying to echo them out.

Namii
01-15-2010, 09:59 AM
Ok i tried that and the Notices are gone. Thank You. But still the table in the database exists and no information is shown to change.

Here's the code at the moment:




<?php


if (isset($_POST['submit']) && $_POST['submit']) {

$id_fornec = $_POST['id_fornec'];
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$empresa = $_POST['empresa'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="editar_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$update = "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone."',e_mail = '".$e_mail."',empresa = '".$_POST[$empresa]."' where id = '".$_POST['id_fornec']."'";

$result = mysql_query($update, $connection);

if (($result)==1){
echo "<p>Dados alterados<br>";
}
}
else {

?>

<form method = "POST" action = "editar_fornec.php">
<fieldset>
<p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
<p align="center" class="style1">ID: <?php if(isset($_POST['id_fornec'])) { echo $_POST['id_fornec']; } ?> <br/>
Nome: <?php if(isset($_POST['nome'])) { echo $_POST['nome']; } ?>
<input type = "text" name = "nome">
</p>
<p align="center" class="style1">Telefone: <?php if(isset($_POST['telefone'])) { echo $_POST['telefone']; } ?>
<input type = "text" name = "telefone">
</p>
<p align="center" class="style1">E-Mail: <?php if(isset($_POST['e_mail'])) { echo $_POST['e_mail']; } ?>
<input type = "text" name = "e_mail">
</p>
<p align="center" class="style1">Empresa: <?php if(isset($_POST['empresa'])) { echo $_POST['empresa']; } ?>
<input type = "text" name = "empresa">
</p>
<p align="center" class="style1">
<input type = "submit" name="submit" value = "Alterar">
</fieldset>
</form>
<?php } ?>

abduraooft
01-15-2010, 10:16 AM
Try
$result = mysql_query($update, $connection) or die(mysql_error());

Namii
01-15-2010, 02:52 PM
Hmm that didn't help.. Also because it doesn't return any errors just.. Doesn't show anything in the form.

abduraooft
01-15-2010, 03:57 PM
Hmm that didn't help.. Also because it doesn't return any errors just.. Doesn't show anything in the form.Looks like you haven't turned error reporting on. If so, do it first by adding
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting (E_ALL);
?> at the top of your code.

Namii
01-15-2010, 04:18 PM
Still doesn't show any errors. It's still the same.

abduraooft
01-15-2010, 04:21 PM
What do you get when you echo your query in the variable $update?

Namii
01-15-2010, 05:28 PM
Nothing. It does nothing i guess :|

Namii
01-17-2010, 04:13 PM
Hi guys,

Since I got no replies in a long time I tried by myself but still i'm getting some errors and I can't make it out.

The errors are:


Warning: Invalid argument supplied for foreach() in C:\wamp\www\editar_clientes.php on line 4

Notice: Undefined variable: formVars in C:\wamp\www\editar_clientes.php on line 8

And my code is



<?php
foreach(@$HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
$db=mysql_connect('localhost', 'root', '');
mysql_select_db("database");
$query="SELECT * FROM clientes WHERE id_clientes = \"".$formVars["id_clientes"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["nome"]=$row["nome"];
$formVars["telefone"]=$row["telefone"];
$formVars["e_mail"]=$row["e_mail"];
$formVars["morada"]=$row["morada"];
$formVars["pontos"]=$row["pontos"];
$formVars["id_clientes"]=$row["id_clientes"];
mysql_close($db);
?>
<html>
<head>
<title>Editar Clientes</title>
</head>
<body>
<col span="1" align="right">
<tr>
<td><font color="blue">Nome:</font></td>
<td><div align="left">
<input type="text" name="nome"
value="<? echo $formVars["nome"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><div align="left"><font color="blue">Telefone:</font></div></td>
<td><div align="left">
<input type="text" name="telefone"
value="<? echo $formVars["telefone"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><font color="blue">E-Mail:</font></td>
<td><div align="left">
<input type="text" name="e_mail"
value="<? echo $formVars["e_mail"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><div align="left"><font color="blue">Morada:</font></div></td>
<td><div align="left">
<input type="text" name="morada"
value="<? echo $formVars["morada"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><font color="blue">Pontos:</font></td>
<td><div align="left">
<input type="text" name="pontos"
value="<? echo $formVars["pontos"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><div align="left"><font color="blue">ID:</font></div></td>
<td><div align="left">
<input type="text" name="id_clientes"
value="<? echo $formVars["id_clientes"]; ?>" size=100>
</div></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</body>
</html>


Thank You!

abduraooft
01-18-2010, 07:19 AM
$result=mysql_query($query) or die(mysql_error());

steve1132
01-18-2010, 10:10 AM
<?php

if ($_POST['submit'])
{

$id_fornec = $_POST['id_fornec'];
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$empresa = $_POST['empresa'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="editar_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$update = "UPDATE fornecedores set nome = '".$nome."',telefone = '".$telefone."',e_mail = '".$e_mail."',empresa = '".$_POST[$empresa]."' where id = '".$_POST['id_fornec']."'";

$result = mysql_query($update, $connection);

if (($result)==1){
echo "<p>Dados alterados<br>";
}
}
else {

?>

<form method = "POST" action = "">
<fieldset>
<p align="center" class="style1"><font color="#0000CC">-= Editar Fornecedores =-</font></p>
<p align="center" class="style1">ID: <?php echo $_POST['id_fornec'];?><br/>
Nome: <?php echo $_POST['nome'];?>
<input type = "text" name = "nome">
</p>
<p align="center" class="style1">Telefone: <?php echo $_POST['telefone'];?>
<input type = "text" name = "telefone">
</p>
<p align="center" class="style1">E-Mail: <?php echo $_POST['e_mail'];?>
<input type = "text" name = "e_mail">
</p>
<p align="center" class="style1">Empresa: <?php echo $_POST['empresa'];?>
<input type = "text" name = "empresa">
</p>
<p align="center" class="style1">
<input type = "submit" name="submit" value = "Alterar">
</fieldset>
</form>
<?php } ?>

Namii
01-18-2010, 11:34 AM
Still gives all variables undefined.

Notice: Undefined index: empresa in C:\wamp\www\editar_clientes.php on line 55
Notice: Undefined index: telefone in C:\wamp\www\editar_clientes.php on line 49
Notice: Undefined index: e_mail in C:\wamp\www\editar_clientes.php on line 52
Notice: Undefined index: nome in C:\wamp\www\editar_clientes.php on line 46
Notice: Undefined index: id_fornec in C:\wamp\www\editar_clientes.php on line 45

Namii
01-19-2010, 03:58 PM
Could anyone help me out please?

It maybe a very easy thing to do but i'm not too familiar with PHP and/or MySQL eather :S

Namii
01-21-2010, 12:56 AM
Already solved it. Thank You to everyone who was kind to try to help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum