简体   繁体   中英

MySql PHP Connection issues

I am aware that this has already been posted but have tried the options available on here, so am forced to ask again...have been trying all sorts for weeks now...to no avail!

Have an account in cPanel, with a database in phpMyAdmin and am trying to return data to my website.

Initially, I kept getting errors connecting, and found that I hadn't set user privileges (am new to this).

Now I have sorted that, when I create a search in the text area and hit return, the Favicon spins around and goes back to the initial state.

I am so spun around with it all now, that I am unsure if its PHP, MYSQL, HTML etc. etc.

Here's the PHP code:

<?php
$output=NULL;   
if(isset($_POST['submit'])){
    //Connect to the database
    $mysqli = new mysqli("localhost", "jj3wg2td_wix", "Sebastian!16", "jj3wg2td_careerslist");
    $search = $mysqli->real_escape_string($_POST['search']);
    //Query the database    
    $result = $mysqli->query("SELECT * FROM careers WHERE Job Title LIKE '%$search%'");
    if($resultSet->num_rows > 0){
        while($rows=$resultSet->fetch_assoc())
        {
            $jobTitle=$rows['Job Title'];
            $jobDesription=$rows['Job Description'];
            $salaryLow=$rows['Salary Low'];
            $salaryHigh=$rows['Salary High'];
            $output .="Job Title:$jobTitle<br />
                        Job Description:$jobDesription<br />
                        Salary Low:$salaryLow<br />
                        Salary High:$salaryHigh<br />
                        <br />";
         }
    }else{
        $output="No results";   
    }       
}
?>

This doesn't return "No Results" at all, just blank space.

I also have a PHP output after my search button code of:

<?php echo $output; ?>

Search button being:

<!--SEARCH BOX-->
    <div class="search-box">
        <form method="POST">
        <input class="search-txt" type="TEXT" name="search" placeholder="SEARCH CAREERS">
            <a class="search-btn" type="SUBMIT" name="submit">
                <i class="fa fa-search" aria-hidden="true"></i>
                    </a>
                        </form>
                            </div>

The 'Local Host' in phpMyAdmin states that the name', Localhost:3306 (have seen this issue a lot). So I have tried that, as well as just 'localhost....but nothing works at all now.

Apologies for my basic understanding in advance...and that you for any help!!!

Completely giving up!

I think your form was not submitting on click of <a> . So, you have to make such changes to submit your form first.

Solution 1 (With help of JS):

<div class="search-box">
  <form method="POST" id="searchForm">
    <input class="search-txt" type="TEXT" name="search" placeholder="SEARCH CAREERS">
    <a class="search-btn" onclick="document.getElementById('searchForm').submit()">
       <i class="fa fa-search" aria-hidden="true"></i>
    </a>
  </form>
</div>

Solution 2: Use <button> as a control for form submitting

    <button class="search-btn" name="submit" type="submit">
      <i class="fa fa-search" aria-hidden="true"></i>
    </button>

or use <input> as submit button,

<input type="submit" name="submit" value="submit" />

The main problem in your code is from PHP side, when you're checking for $_POST['submit'] here,

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

It's not entering in the if() statement, because it's not getting $_POST['submit'] in you're code.

Please make sure you're following naming conversations for datatables & fields. So, please don't use uppercase & space in table/field name. Like you used in your query ("SELECT * FROM careers WHERE Job Title LIKE..."). Here Job Title should be job_title . (***Update in data-table too)

$mysqli->query("SELECT * FROM careers WHERE job_title LIKE '%$search%'");

Hope this will help you.

there are no object assigned to the variable $resultSet in your code. but you try to read it's property like $resultSet->num_rows this.

use $result insted of $resultSet

try this

$output=NULL;

if(isset($_POST['submit'])){
//Connect to the database
$mysqli = new mysqli("localhost", "jj3wg2td_wix", "Sebastian!16", "jj3wg2td_careerslist");
    
$search = $mysqli->real_escape_string($_POST['search']);
    
//Query the database    
$result = $mysqli->query("SELECT * FROM careers WHERE Job Title LIKE '%$search%'");
    if($result->num_rows > 0)
    {
        while($rows=$result->fetch_assoc())
        {
            $jobTitle=$rows['Job Title'];
            $jobDesription=$rows['Job Description'];
                $salaryLow=$rows['Salary Low'];
                    $salaryHigh=$rows['Salary High'];
            
            $output .="Job Title:$jobTitle<br />
                        Job Description:$jobDesription<br />
                        Salary Low:$salaryLow<br />
                        Salary High:$salaryHigh<br />
                        <br />";
        }
    }
    else{
        $output="No results";   
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM