0

i recently posted but i do not think i explained it to well.

I have a page products page the url is for example http:.......rest_id=3/area='Enfield'. I have been using similar query throughout my website but this one is not working, even though it was working for about 2 minutes earlier.

I am using a GET statement to get the area name which is in the url, then from there i use the area name to get the correct data from the DB, such as phone number, twitter and Facebook handles. but nothing is showing.

I have error handlers, the only error i have is Notice: Undefined variable: twit in... For all 3 variables, even though they have been declared.

Any ideas why this may be happening.

  if (isset($_GET['area'])) {
                    $franc_dets = $_GET['area'];
                    $get_franc_d = "SELECT * FROM Franc_dets WHERE Fran_City = '$franc_dets'";
                    mysqli_stmt_execute($run_get_franc_d);
                    $iii = 1;

                    while ($row_get_fram = mysqli_fetch_array($run_get_franc_d)) {
                        $fran_phone = $row_get_fram['Fran_Contact_Num'];
                        $twit = $row_get_fram['Twitter'];
                        $iii++;

HTML

   ......
    <td class='phn_dets'>
                        <p id='phn_title'>Problems ordering?</p>
                        <p id='phn_numb'><?php echo $fran_phone ?></p>
                    </td> 
                    <td class='collapse'>
                        <img src='./Images/franc_dets_twitter.png'  height='50' width='50' alt='Twitter logo'>    
                    </td>


                    <td class='twitter_dets'>
                        <p id='sm_title'>Social media</p>
                        <a id='sm_twit' href='https://twitter.com/<?php echo $twit ?>'>@<?php echo $twit ?></a>
                    </td>

                     <td class='collapse'>
                     <img src='./Images/franc_dets_fb.png'  height='50' width='50' alt='Facebook logo' >    
                    </td>
                    <td class='fb_dets'>

                <a id='sm_fb' href='https://www.facebook.com/<?php echo $fb; ?>'><?php echo $fb; ?></a>
9
  • Have you tried using mysqli_fetch_assoc instead of mysqli_fetch_array? Commented May 8, 2016 at 2:21
  • @evsar3 yes i have, unfortunately the result is no different Commented May 8, 2016 at 2:30
  • What happened when you used var_dump($twit); inside the while loop? Commented May 8, 2016 at 2:34
  • instead of creating a new question, that is just as vague as the 1st, you should just update the original question - stackoverflow.com/questions/37094876/… Commented May 8, 2016 at 2:38
  • @enhzflep nothing changes, prints the same notice Commented May 8, 2016 at 2:50

1 Answer 1

3

You declare $twit within the while loop. If your query returns no results, then $twit is never defined, therefore the notice you see. In your case I see 2 reasons why you would get no results even if there is data in your DB:

  1. There is quotes in your URL and in your queries, which can conflicts and make the query fail.
  2. You put your query in mysqli_stmt_execute() which expect a Mysqli Statement, not a string. You have to pass your query in mysqli_prepare(). Then put the result in mysqli_stmt_execute()

Make sure to handle properly the case where no results are returned, which can be as simple that giving a default value to $twit outside the while loop or tell the user that there is no results.

Also, the way you put the variable in your query makes your website vulnerable to SQL injection. I suggest you read on the subject. Use prepared queries with placeholders. See the examples in the PHP manual

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.