Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you know what the <code>datetime</code> value and the <code>username</code> values are then you can simply use:</p> <pre><code>SELECT * FROM orders WHERE username = '$username' AND datetime = '$datetime' </code></pre> <p>However, what you would be better off doing is splitting this into two separate tables; something like:</p> <pre><code>Orders OrderID OrderTime UserName Items ItemID OrderID Title </code></pre> <p>Then you would search in the following way:</p> <pre><code>SELECT Orders.OrderID, Orders.UserName, Items.Title FROM Orders INNER JOIN Items ON Orders.OrderID = Items.OrderID WHERE Orders.UserName = '$username' AND Orders.OrderDate = '$datetime' </code></pre> <p>When adding orders you add a record to <code>Orders</code> first, and then use that <code>OrderID</code> and add it to each item inserted in <code>Items</code>...</p> <h2>Insert Example</h2> <pre><code>$mysqli; //Assuming your connection to the database... $items; //Assuming an array of items for the order like: array('Coffee', 'Tea') $username; //Assuming the user name to be inserted for the order $mysqli-&gt;query("INSERT INTO Orders(`OrderTime`, `UserName`) VALUES(NOW(), '$username')"); $orderid = $mysqli-&gt;insert_id; foreach($items as $item){ $mysqli-&gt;query("INSERT INTO Items (`OrderID`, `Title`) VALUES($orderid, '$title')"); } </code></pre> <p><strong>NOTE:</strong> You should make sure to sanitize data before inserting to database...</p> <hr> <h2>Storing JSON</h2> <p>Storing <code>JSON</code> in a database is going to require you to make sure that you use a field data type that is an appropriate length (e.g. a <code>blob</code>).</p> <p>You mentioned that you retrieve the <code>titles</code> as an array from a form so I'm now going to refer to that as <code>$titles</code>.</p> <h3>Saving to database</h3> <pre><code>$username = '...'; // Username or id to store in database with order $titles = array(.....); // Array of titles from form $encodedTitles = json_encode($titles); // Convert to JSON $mysqli-&gt;query("INSERT INTO table_name (titles_field, username_field, date_field) VALUES ('$titles', '$username', NOW())"); // Save to database (assuming already open connection </code></pre> <h3>Retrieve from database</h3> <pre><code>$result = $mysqli-&gt;query("SELECT titles FROM table_name WHERE username = 'username_value' AND date_field = 'date_value'"); //Run query to get row $row = $result-&gt;fetch_assoc(); // Fetch row $titles = json_decode($row['titles']); // This is the same as the `titles` array from the from above! </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload