Hey guys!
Right heres the problem, I have made this script that basically displays all the data from a table from MySQL; pricelist.
However it only displays the data when i have to manually write in all the database passwords and names and stuff, however when i use the require_once() function the damn thing doesnt work, and since having all the passwords and stuff on the actual code is a security risk i need to use the require_once() function.
The code with the require_once() function doesnt even display a table, just the headers of the page...
Code without require_once() function:
<h1>Update Items</h1>
<p>Update items below.</p>
<?php
$db_host = 'localhost';
$db_user = 'X';
$db_pwd = 'X';
$database = 'http://localhost';
$table = 'pricelist';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db(moretonale))
die("Can't select database");
// sending query
$sql="SELECT * FROM $table";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Item</strong></td>
<td align="center"><strong>Price</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td>
<td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
//define each variable
$item= $_POST['item'][$i];
$price = $_POST['price'][$i];
// Check if button name "Submit" is active, do this
if($Update){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $table SET item='$item', price='$price' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:pricelistdata.php");
}
mysql_close();
}
?>
Code with require_once() funtion:
<h1>Update Items</h1>
<p>Update items below.</p>
<?php
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
//if so, connect to database
require_once('../sqlconnect/connect.php');
// sending query
$sql="SELECT * FROM pricelist";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="pricelistdata" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Item</strong></td>
<td align="center"><strong>Price</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td>
<td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?
//define each variable
$item= $_POST['item'][$i];
$price = $_POST['price'][$i];
// Check if button name "Submit" is active, do this
if($Update){
for($i=0;$i<$count;$i++){
$sql1="UPDATE pricelist SET item='$item', price='$price' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:pricelistdata.php");
}
mysql_close();
}
?>
Does anyone have any ideas as to why this isn't worling?
Cheers in advance