-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
In an attempt to remove confusion, here is a whole script. <?php include 'db_inc.php'; // use your own $db = myConnect('jimr'); // mysqli connection if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt = $db->prepare("INSERT INTO player(uniform, nameFirst, nameLast) VALUES (?,?,?)"); $stmt->bind_param('sss', $uniform, $fname, $lname); foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $stmt->execute(); } header("Location:#"); exit; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css"> <title>Example</title> <meta name="creation-date" content="11/23/2020"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $("#add-player").click( function(e) { e.preventDefault() $("#myRepeatingFields").append('<div class="entry input-group col-xs-3">' + '<input class="form-control" name="uniform[]" type="text" placeholder="Uni #" /> ' + '<input class="form-control" name="nameFirst[]" type="text" placeholder="First Name" /> ' + '<input class="form-control" name="nameLast[]" type="text" placeholder="Last Name" />' + '</div>'); }) }) </script> </head> <body> <form action="" method="post"> <div id="myRepeatingFields"> <div class="entry input-group col-xs-3"> <input class="form-control" name="uniform[]" type="text" placeholder="Uni #" /> <input class="form-control" name="nameFirst[]" type="text" placeholder="First Name" /> <input class="form-control" name="nameLast[]" type="text" placeholder="Last Name" /> </div> </div> <button id='add-player' title='add new player entry'><i class='fas fa-plus-circle'></i></button> <input type="submit" value="Send Roster" name="submit"> </form> </body> </html> The table used is... CREATE TABLE `player` ( `player_id` int(11) NOT NULL AUTO_INCREMENT, `uniform` varchar(45) DEFAULT NULL, `nameFirst` varchar(45) DEFAULT NULL, `nameLast` varchar(45) DEFAULT NULL, PRIMARY KEY (`player_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -
The three dots are significant, expanding the array into a comma separated list of values. They won't work with a PHP version before v5.6, but if that's case then you have more work to do bringing your PHP up to date (v7.4). Have you turned your error reporting and display on yet?
-
That is not the same as the code I posted and should be generating several errors - if you have your error display/reporting turned on.
-
You were close with your original method but it needed a couple of tweaks while fetch { if $type != $t { if $t != '' output </optgroup> // only close previous group if there was one output <optgroup> $t = $type end if output <option> end while output </optgroup> // close final group
-
I have an inflateable baseball bat which I often use on such occasions.
-
To expand on what @kicken said, numeric values do not get quoted and string values only get quoted if they contain one or more commas. If you really do need everything quoted then you could... function generateCSV($data, $filename) { $fp = fopen($filename, 'w'); foreach ($data as $k => $v) { if ($k==0) { fputcsv($fp, array_keys($v)); } fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n", ...array_values($v)); } fclose($fp); } OUTPUT: Date,time,Device,label,throughput_read,throughput_write "11/21/2020","00:00","audigysg36s6101","192905199","1742118968","11.073020990149" "11/21/2020","00:15","audigysg36s6101","192914200","1742253920","11.072679922568"
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
Using your original form, and mysqli ... if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt = $db->prepare("INSERT INTO player(uniform, nameFirst, nameLast) VALUES (?,?,?)"); $stmt->bind_param('sss', $uniform, $fname, $lname); foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $stmt->execute(); } } -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
$data = []; foreach ($_POST['uniform'] as $k => $uniform) { $fname = $_POST['nameFirst'][$k]; $lname = $_POST['nameLast'][$k]; $data[$k] = [$uniform, $fname, $lname]; } // to view the $data echo '<pre>' . print_r[$data, 1] . '</pre>'; // insert each $data element into your table (as shown) foreach ($data as $rec) { // insert record } -
try $arr = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); $prev = ''; $results = []; $j = 0; foreach ($arr as $i => $v) { if ($v != $prev) { $j = $i; $results[$j] = 1; $prev = $v; } else { $results[$j]++; } } foreach ($results as $k => $v) { echo "$arr[$k] : $v <br>"; } Result: test : 2 hello : 1 test : 1 world : 3 hello : 1 test : 1
-
You've had some clues. What have you tried?
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
It was relevant to your problem - if you were using PDO (Sorry, not clairvoyant) Any code given are examples, not necessarily writing your code for you. Rather that you should emulate, not copy. The onus is on you read them, see what they are doing and apply to your situation. -
if array == array[i-1] then you have adjacent duplicates
-
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
Had you consider starting with an index of 0, then, whenever you add a new player, add 1 to get the index of the new array? This way you can have as few or many as you like. As for your error, have you just blindly copy/pasted my coded without regard to whatever variable contains your mysql connection - or even if yours is pdo or mysqli? -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
So? To insert $stmt = $pdo->prepare("INSERT INTO mytable (uniform, nameFirst, nameLast) VALUES (:uniform, :nameFirst, :nameLast)"); foreach ($_POST['data'] as $data) { $stmt->execute($data); } -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
Alternatively, a change to your input naming... <?php if (isset($_GET['data'])) { echo '<pre>', print_r($_GET, 1), '</pre>'; } ?> <form> <input class="form-control" name="data[1][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[1][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[1][nameLast]" type="text" placeholder="Last Name" /> <br> <input class="form-control" name="data[2][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[2][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[2][nameLast]" type="text" placeholder="Last Name" /> <br> <input class="form-control" name="data[3][uniform]" type="text" placeholder="Uni #" /> <input class="form-control" name="data[3][nameFirst]" type="text" placeholder="First Name" /> <input class="form-control" name="data[3][nameLast]" type="text" placeholder="Last Name" /> <br> <input type='submit' name='btnSub' value='Submit'> </form> giving GET = Array ( [data] => Array ( [1] => Array ( [uniform] => 1 [nameFirst] => aaa [nameLast] => bbb ) [2] => Array ( [uniform] => 2 [nameFirst] => ccc [nameLast] => ddd ) [3] => Array ( [uniform] => 3 [nameFirst] => eee [nameLast] => fff ) ) [btnSub] => Submit ) -
Trying to get data from form with Repeatable fields into MySQL...
Barand replied to Jim R's topic in MySQL Help
Something like this?... <?php $data = []; foreach ($_GET['uniform'] as $k => $uniform) { $fname = $_GET['nameFirst'][$k]; $lname = $_GET['nameLast'][$k]; $data[$k] = [$uniform, $fname, $lastname]; } ?> -
yes, and make sure your php error reporting is on.
-
I don't see any error check after the query() call. Easier than checking all mysqli calls is to put this line of code before the "new mysqli()" line ... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
-
What if you echo the query string to examine the syntax? echo "SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'";
-
Check for mysql error messages print_r($conn->errorInfo()) // PDO echo $conn->error; // mysqli
-
Name your inputs as name='str[1]', name='str[2] etc instead os str1, str2 ... Then it's a simple loop when you process the posted data foreach ($_POST['str'] as $i => $value) { if (trim($value) == '') { echo "$i is blank</br>"; // or whatever you want to do with empty ones } }
-
Telling us what the problem is goes a long way towards getting it resolved.