slyme 27 Junior Poster in Training

I believe that it should look something like this:

$my_conn = new mysqli('localhost', 'user name', 'password', 'database name');
$sql = "SELECT * FROM messages";
$result = $my_conn->query($sql);
while ($obj = $result->fetch_object()) {
    echo = $obj->message;
}

I could easily be wrong though ...

hope this helps,

Simon.

slyme 27 Junior Poster in Training

Try this, it processes everything before updating the database:

<?php
// Set your variables to empty if they haven't been submitted:
if (isset($_POST['First_Name'])) {
    $First_Name = $_POST['First_Name'];
} else {
    $First_Name = '';
}
if (isset($_POST['Last_Name'])) {
    $Last_Name = $_POST['Last_Name'];
} else {
    $Last_Name = '';
}

if (isset($_POST['Email'])) {
    $Email = $_POST['Email'];
} else {
    $Email = '';
}
if (isset($_POST['Code'])) {
    $Code = $_POST['Code'];
} else {
    $Code = '';
}
if (isset($_POST['Phone'])) {
    $Phone = $_POST['Phone'];
} else {
    $Phone = '';
}
// Create message variables:
$outputMessage = '';
$errorMessage = '';
// Check if submit clicked:
if (isset($_POST['submit'])) {
    // Check fields are filled in:
    if (empty($First_Name)) {
        $errorMessage .= "<li>You forgot to enter your First Name</li>";
    }
    if (empty($Last_Name)) {
        $errorMessage .= "<li>You forgot to enter your Last Name</li>";
    }
    if (empty($Email)) {
        $errorMessage .= "<li>You forgot to enter your Email</li>";
    }
    if (empty($Code)) {
        $errorMessage .= "<li>You forgot to enter your Wowcher code you silly person!</li>";
    }
    if (empty($Phone)) {
        $errorMessage .= "<li>ou forgot to enter your Phone Number</li>";
    }
    // Check for error
    if (empty($errorMessage)) {
        // No error, do your database thing:
        mysql_connect ("localhost", "root", "root") or die ('Error: ' . mysql_error());
        mysql_select_db ("wowcher") or die ('Error: ' . mysql_error());
        $query="INSERT INTO wowcher_code(First_name, Last_Name, Email, Code, Telephone)VALUES ('".$First_name."', '".$Last_Name."', '".$Email."', '".$Code."', '".$Phone."')";
        mysql_query($query) or die ('Error updating database because: '.mysql_error());
        mysql_close();
        // Empty the variables and set output message:
         $First_Name = '';
         $Last_Name = '';
         $Email = '';
         $Code = ''; …
slyme 27 Junior Poster in Training

Bit of a long shot ... what happens if you set the header location to http//www.website.com/$url ?

slyme 27 Junior Poster in Training

create a variable before the loop and set its value to 1

inside the loop, call your radio button something like my_button_X where X is the value created above.

then add one to the variable before ending the loop.

You could also send the final value of the variable as a hidden field which can be useful when processing the radio button inputs.

How to use while loop to loop a set of radio button groups? I have several questions that retrieve from database and each question has five different ratings named 1, 2, 3, 4 and 5. I create radio button group for the rating and using while to loop them. I know I have to create a unique name for radio buttons for each row but how to increment the value automatically for each row? I didn’t retrieve any value from database when I create the radio button group. All the data is arrange in the table. Besides, I need to save each selected value into database. I am using php. Anyone got ideas for my question. Thanks for your kindly reply.

slyme 27 Junior Poster in Training
slyme 27 Junior Poster in Training

Ardav is correct Motorider, you do not have a variable function so that's not the problem.

Here's what I would do at this point:

Using phpMyAdmin or some MySQL tool I would simply run the select query and check for results. If that doesn't help, I would then replace all of the code inside the if statement with:

$search_result = 'form submitted';

Then I would submit the form just to check that side of it is working (which it should be).

One of these tests should highlight the problem. Perhaps you could add an if statement inside the database query to return 'no records found' if the search result is genuinely empty.

Let us know how it goes ...

Simon.

Motorider commented: He helped me out tremendously +1
slyme 27 Junior Poster in Training

Here you go Motorider - I have set the form action empty, this has the same effect as your code above.

I cannot check if the code runs properly once the form button is clicked because, obviously, I don't have the database.

<?php
	
//Create an empty variable
$search_result = '';

//If statement, to check if the submit button has been clicked.
if (isset($_POST['submit'])) {
	//Connect To Database
	$hostname='';
	$username='';
	$password='';
	$dbname='';

	mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
	mysql_select_db($dbname);
	
	$mytype = $_POST['mytype']; //this is an integer
	$term = $_POST['desc']; //this is a string
	
	$sql = mysql_query("select * FROM products WHERE description like '%$term%' AND product_type_id = '{$mytype}'");
	
	while ($row = mysql_fetch_array($sql)) {
		// Use the variable to build the result
		$search_result .= 'ID: '.$row['ID'] . "\n";
		$search_result .=  '<br/> Part No: '.$row['part no'] . "\n";
		$search_result .=  '<br/> Description: '.$row['description'] . "\n";
		$search_result .=  '<br/> Price: '.$row['price'] . "\n";
		$search_result .=  '<br/><br/>' . "\n";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bus Parts Search</title> 
<link href="test.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {font-size: 12pt}
.style2 {font-size: 12px}
-->
</style>
</head>

<body>
<div>
<div class="banner"><img src="graphics/stainlessbg2copy.jpg" width="950" height="125"></div>
    
<div class="cont"></div>

<div class="head1">
  <h4><u>Search Parts Here</u></h4>
  <h4 class="style1">First Select type. </h4>
  <h4 class="style1">Enter part description keyword<br>
  and submit.</h4>
  <h4 class="style1">To see a complete list of parts<br>
    on hand for a type.<br>
    Submit with description blank.</h4>
  </div>

<div class="search"> …
slyme 27 Junior Poster in Training

The variables $table and $suburb will be empty when the page is first loaded. They will only populated once the form has been submitted - whatever other problems there may be, I think this should be avoided by using an if statement to check that the form has been submitted before executing any other code.

slyme 27 Junior Poster in Training

Ok - surround the code with an if statement to check whether the form has been posted or not.

I'm guessing that the code is being executed without any input when you first load the page.

slyme 27 Junior Poster in Training

You can use this script to test your db/php stuff - fill in the blanks at the top and run it, if it fails you should glean some more meaningful info:

<?php

$db_host = ''; 
$db_name = '';
$db_user = '';
$db_password = '';


$db_table = '';
$db_query = '';
$db_field = '';
$db_display = '';


$con = mysql_connect($db_host, $db_user, $db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_name, $con);
$result = mysql_query("SELECT * FROM $db_table WHERE $db_field = '$db_query'");

while($row = mysql_fetch_array($result))
  {
  echo $row[$db_display];
  }


mysql_close($con);
?>
slyme 27 Junior Poster in Training

Try this:

("SELECT * FROM '" . $table . "' WHERE Suburb = '" . $suburb . "'")

slyme 27 Junior Poster in Training

Stick the PHP code right at the top of the page (just to keep things neat and tidy really) above the HTML stuff.

Create an empty variable, say $output_result before your existing code.

Now, surround your code (not the empty variable) with an if statement. The if statement should test whether or not the Submit button has been clicked.

Instead of using echo in your code use the empty variable and fill it with the result.

Finally, echo the empty (or not!) variable in the div where you want to display the results.

Remember to set the form action to refer back to the same page.

hope this helps ...
Simon

slyme 27 Junior Poster in Training

Is $suburb text or numeric? If it is text it needs the single quotes around it.
If it is numeric then you don't need the quotes.
Either way, as Thirusha says, you do need the curly brackets (plus the double quote on the end).