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

Recommended Answers

All 5 Replies

Member Avatar for fatihpiristine

how did you do that require_once()

just put it in the middle of the page?

actually works like this:
require_once('somepage.php');

No theres two different peices of code there that do the same thing, one without the require_once() and one with.

The one that has the require_once() has it at the start of the code after the

if (isset($_POST)) {

I just can't figure out why the one with the require_once() function does not work.

Member Avatar for fatihpiristine

as i said before it doesnt work in that way

well., no it does, because it's worked on every other script that i've done, just not this one.

Could you give me details as to what i should do to correct it?

Make sure you include it before you're trying to act on db and make sure you're using the correct path. The best idea is to use an absolute path (also see $_SERVER)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.