0

I've been looking everywhere but I can't seem to find a solution to my Problem. I have a table that is editable with JEditable, what I want to do is add a dropdown menu to it that gets its values from a json file in which I make a SQLI query to get the data. The Code for using the json file is from JEditable and works if I use an Array in plain text in the json. if I want to use the Query it breaks though. Any advice?

My Edit.js file has this:

   $(".editable-select-REG").editable("save.php", {
        type   : "select",
        loadurl : "REG.php",
        loadtext : "Fetching JSON…",
        submit : "OK",
        style  : "inherit"
    });

My HTML Dropdown is this: (just for clarification this works with the plaintext Array so no Need to Change this i guess)

    <td rowspan="2"><span class="editable-select-REG" name="REG"/><?php echo $row ['reg'] ?></td>

This is where the Problem in my json.php lies: This works:

    <? php
     $array['test1'] =  'test1';
     $array['test2'] =  'test2';
     $array['test3'] =  'test3';
     $array['test4'] =  'test4';
     $array['selected'] =  'selected one';
     echo json_encode($array);
     ?>

This is what i want basically:

 <?php
 include('config.php');

$sql = "SELECT reg FROM lfz";

$result = mysqli_query($db, $sql) or die("Error: ".mysqli_error($db));;

$regs = array();

while ($row = mysqli_fetch_array($result))
{
    array_push($regs, $row["reg"]);
}

echo json_encode($regs);
?>

As I said the basics work i just need help with the json.php file to get my values from the database.

  • 1
    what is the result of your query? have you debug it? – Sfili_81 Jul 14 '20 at 06:39
  • Well the query works in php my admin :D Problem is i dont have any form of console on this PC since it's a work PC and they disabled it. Can't even do 'inspect element' in the browser. BTW i Need to use IE and Edge...…. – Chris Pyro Jul 14 '20 at 06:41
  • You don't need the whole `while` loop and an empty array because when you query the database you get the data in the form of array. – Kunal Raut Jul 14 '20 at 06:43
  • Can you post a Code snippet on how to rewrite that? I'm fairly new to this :D – Chris Pyro Jul 14 '20 at 06:49
  • try to var_dump($regs). If you are new to this it's a good point to start to learn by yourself. – Sfili_81 Jul 14 '20 at 06:50
  • The SQL works and it does give me the correct Array. The select just somehow doesn't fetch the Array if it's not plainy written – Chris Pyro Jul 14 '20 at 08:24
  • OMFG the reason it didnt work was that i commented out the plain Array i wrote…. i deleted it and now it works… – Chris Pyro Jul 14 '20 at 09:01

1 Answers1

-1

I just used this Code for the Array:

<?php
include('config.php');

$sql = "SELECT reg FROM lfz";

$result = mysqli_query($db, $sql) or die("Error: ".mysqli_error($db));;

$regs = array();
while ($row = mysqli_fetch_array($result))
{
    $regs[$row['reg']] = $row['reg'];
}
echo json_encode($regs);
?>

It did give me the correct Array from the beginning. the reason it didnt Show up in the Dropdown was because the old Array was somehow interfering. I deleted it and now it works.

  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jul 14 '20 at 22:47