PDA

View Full Version : database update won't work


UD2006
05-13-2009, 03:38 PM
I try to update a mysql database from input text fields, but it won't update.

First I select with a selection menu the database items ( they are put into the text fields). Then I change the items that need an update in the text fields and click on update to update them into the database.

But checking the database, there is no update of the info.

I really don't know why this isn't working.

Here is the php file I use:

<html>
<head>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
background-color: #E8F8FF;
font-size: 13px;
}

.orangebutton{
background-image: url(../bestellingendatabase2/img/orangebtn.jpg);
background-repeat: no-repeat;
text-align: center;
color: #660000;
border: 1px solid #bf6628;
height: 20px;
font-size: 10px;
font-weight:bold;
font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
background-position: left top;
padding-left: 6px;
padding-right: 6px;
margin-left: 2px;
margin-right: 2px;
text-decoration: none;
margin-top: 0px;
}

.important{
color: Red;
}
-->
</style>
</head>
<body>
<?PHP
include "connect.php";
$sql = 'SELECT * FROM hesk_suppliers ORDER BY leverancier ASC';
$result = mysql_Query($sql) or die ('Error in query: $query. ' . mysql_error());

$menuContent2 = ''; $menuArray2 = '';
while ( $row = mysql_fetch_array( $result ) ) {
$menuArray2 .= " dArray['$row[Leverancier]'] = ['$row[LeverancierID]','$row[Adres]','$row[Crediteuren]','$row[Postcode]','$row[Land]','$row[Plaats]','$row[Telefoon]','$row[Emailleveran]','$row[Contactpers1]','$row[Contactpers2]'];\n";
$menuContent2 .= " <option value=\"$row[Leverancier]\">$row[Leverancier]</option>\n";
}
?>
<script type="text/javascript">
var dArray = new Array();
<?PHP echo $menuArray2; ?>

function setVal2(obj2) {
var levFld = document.getElementById('Leverancier');
var levidFld = document.getElementById('LeverancierID');
var addressFld = document.getElementById('Adres');
var creditFld = document.getElementById('Crediteuren');
var zipFld = document.getElementById('Postcode');
var lndFld = document.getElementById('Land');
var cityFld = document.getElementById('Plaats');
var teleFld = document.getElementById('Telefoon');
var elFld = document.getElementById('Emailleveran');
var cp1Fld = document.getElementById('Contactpers1');
var cp2Fld = document.getElementById('Contactpers2');

if(obj2 == 'select name2') {
levFld.value = '';
levidFld.value = '';
addressFld.value = '';
creditFld.value = '';
zipFld.value = '';
lndFld.value = '';
cityFld.value = '';
teleFld.value = '';
elFld.value = '';
cp1Fld.value = '';
cp2Fld.value = '';
}
else {
levFld.value = obj2;
levidFld.value = dArray[obj2][0]; // 0 is the first item in the second array. 1 is the second, etc. Array keys are numerical by default.
addressFld.value = dArray[obj2][1];
creditFld.value = dArray[obj2][2];
zipFld.value = dArray[obj2][3];
lndFld.value = dArray[obj2][4];
cityFld.value = dArray[obj2][5];
teleFld.value = dArray[obj2][6];
elFld.value = dArray[obj2][7];
cp1Fld.value = dArray[obj2][8];
cp2Fld.value = dArray[obj2][9];
}
}

window.onload = function() {
document.getElementById('nameid2').onchange = function() {setVal2(this.options[this.selectedIndex].value)};
}

</script>
<!-- Leverancier gegevens -->
<font family='Arial' size='4'><b>Wijzig hier de gegevens van een leverancier</b></font><br><font class="important"><i>Verplichte velden zijn gemarkeerd met *!</i></font>
<hr>
<table border="0" width="100%">
<tr>
<td></td>
<td><select id="nameid2" name="name2">
<option name="select name2" value="select name2">Selecteer leverancier</option>
<?PHP echo $menuContent2; ?>
</select>
</td>
</tr>
<tr>
<td><form action='wijzigen.php' method='post'></td>
</tr>
<tr>
<td>LeverancierID:</td>
<td><input type='text' name='LeverancierID' style='background:#eee none; color:#222; font-style: italic' size='10' readonly='readonly'></td>
</tr>
<tr>
<td>Leverancier: <font class="important"><b>*</b></font></td>
<td><input type='text' name='Leverancier' size='40'></td>
</tr>
<tr>
<td>Adres:</td>
<td><input type='text' name='Adres' size='40'></td>
</tr>
<tr>
<td>Crediteuren:</td>
<td><input type='text' name='Crediteuren' size='10'></td>
</tr>
<tr>
<td>Postcode:</td>
<td><input type='text' name='Postcode' size='10'></td>
</tr>
<tr>
<td>Land:</td>
<td><input type='text' name='Land' size='10'></td>
</tr>
<tr>
<td>Plaats:</td>
<td><input type='text' name='Plaats' size='30'></td>
</tr>
<tr>
<td>Tel:</td>
<td><input type='text' name='Telefoon' size='20'></td>
</tr>
<tr>
<td>Email: <font class="important"><b>*</b></font></td>
<td><input type='text' name='Emailleveran' size='40'> <font class="important">Dit veld is verplicht i.v.m. het versturen van de bestelling naar de leverancier.</font></td>
</tr>
<tr>
<td>Contactpersoon 1:</td>
<td><input type='text' name='Contactpers1' size='30'></td>
</tr>
<tr>
<td>Contactpersoon 2:</td>
<td><input type='text' name='Contactpers2' size='30'></td>
</tr>
<tr>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<td><input type='submit' name='submit' value='Wijzigen' class='orangebutton'>
</form>
</td>
</tr>
</table>
</body>
</html>


And here the wijzigen.php file:

<?php
$db = mysql_connect("localhost", "root", "") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("bestellingen",$db))
die("No database selected.");

$LeverancierID=$_POST['LeverancierID'];
$Leverancier=$_POST['Leverancier'];
$Adres=$_POST['Adres'];
$Crediteuren=$_POST['Crediteuren'];
$Postcode=$_POST['Postcode'];
$Land=$_POST['Land'];
$Plaats=$_POST['Plaats'];
$Telefoon=$_POST['Telefoon'];
$Emailleveran=$_POST['Emailleveran'];
$Contactpers1=$_POST['Contactpers1'];
$Contactpers2=$_POST['Contactpers2'];

$query = "UPDATE hesk_suppliers SET Leverancier='$Leverancier', Adres='$Adres', Crediteuren='$Crediteuren', Postcode='$Postcode', Land='$Land', Plaats='$Plaats', Telefoon='$Telefoon', Emailleveran='$Emailleveran', Contactpers1='$Contactpers1', Contactpers2='$Contactpers2', WHERE LeverancierID=$LeverancierID";
if (mysql_query($query))
{
echo('<SCRIPT>
alert("Gegevens van ' .$Leverancier. ' zijn bijgewerkt."); location.href("index.php?a=add");</SCRIPT>');
}
?>


Any help would be great, thanks.

abduraooft
05-13-2009, 03:55 PM
Add proper error check to your query like
$result=mysql_query($query) or die(mysql_error());
if($result){
//proceed
}

Also, it would be a good idea to echo your $query before passing to your mysql_query() function, and then execute the generated output from mysql console or via phpmyadmin

UD2006
05-13-2009, 04:04 PM
I solved it, I had a comma before the WHERE.

Once removed the query worked.

Also I changed the

if (mysql_query($query))


back to:

$result = mysql_query($query);


Thanks anyway for your reply.