webdeveloper123
Members-
Posts
437 -
Joined
-
Last visited
-
Days Won
1
Everything posted by webdeveloper123
-
It's not a unknown variable. I have a SQL Select statement which for the result set I do this after: $result = mysqli_query($link, $queryselect); $table = []; while ( $row = mysqli_fetch_assoc( $result ) ) { $table[] = $row; } Then I do this: if ( count($table) == 0) { exit; } else { $first_name = $table[0]["FirstName"]; $last_name = $table[0]["LastName"]; $email = $table[0]["Email"]; $age = $table[0]["Age"]; $birthday = $table[0]["Birthdate"]; $favlanguage = $table[0]["FavLanguage"]; $VehSelection= $table[0]["VehSelection"]; } But I am looking and mac_gyvers and Barands foreach loop and trying to come up with something
-
I wanted to store in UK date format I agree, I was going to fix that later, I only realised long after I created the db I agree, again I was a bit rusty when I created my database, but it's only an example to practice php/mysql so If it was a professional solution I would change it Again, I agree but this is just an exercise to practice programming, the form won't change to that extent. With your example it is all in PDO, and I am doing MySqli. I know your solution is more professional and elegant but I just want to fix this part and get to the next stage. Is there something you can do with my existing code...surely it's not that bad that It cannot be used, even though it might not be great, it surely can still be used?
-
Thanks benanamen & mac_gyver but that's a little too advanced for me, only been doing php for 4 months now. The code I previously posted was not very good, here is a better version: <?php if (!empty($VehSelection)) { foreach($table as $key => $value) { $VehSelection= $value["VehSelection"]; ?> <input type="checkbox" id="yacht" name="vehicle[]" value="Yacht" <?php echo ($VehSelection == 'Yacht') ? 'checked' : ''; ?> /> <label for="yacht" class="boxstyle"> I have a yacht</label><br> <input type="checkbox" id="superCar" name="vehicle[]" value="SuperCar" <?php echo ($VehSelection == 'SuperCar') ? 'checked' : ''; ?> /> <label for="superCar" class="boxstyle"> I have a super car</label><br> <input type="checkbox" id="plane" name="vehicle[]" value="Plane" <?php echo ($VehSelection == 'Plane') ? 'checked' : ''; ?> /> <label for="plane" class="boxstyle"> I have a plane</label><br><br><br> <?php }} ?> This works, but If there are 2 problems. With one value(say for example "Plane"), it will generate 3 check boxes with "Plane" ticked(which is great, that works!). But with 2 values, (say "SuperCar" and "Yacht" it will print me 6 check boxes) and with all 3 values, it will print me 9 check boxes. Can anyone figure out where i'm going wrong please?
-
I do have a database Barand, it looks like this: Form (FormId, Name, Lastname, age, birthday, email, FavLanguage, Vehicle) Vehicle (VehicleId, FormId (FK), VehSelection) Sample data from Form table: Sample data from Form Table: 191 tom smith 33 09-06-1997 tom@smith.com CSS NULL 192 Frank Lampard 39 10-06-1992 frank@everton.com CSS NULL 193 John Atkins 23 11-07-2006 john@gmail.com JavaScript NULL Sample data from Vehicle table 216 191 Plane 217 192 Yacht 218 192 SuperCar 219 192 Plane 220 193 SuperCar 221 193 Plane What I want to acheive: On every "Edit form" page, 3 check boxes always on display, and the relevant ones checked for that record. That way a user can come back a week later and decide "Actually I haven't got a plane anymore, but I do have a yacht. Untick plane, tick yacht and update.
-
The thing is if one check box was ticked initially, the loop runs once. If 2 were checked, the loop runs twice and 3 check boxes ticked it runs 3 times. That's what I think is happening. But If I take it out the loop then I only get 1 check box ticked when say for example that user had ticked 2 or 3
-
I am creating a edit form. On the insert form there are 3 check boxes, each relating to what type of Vehicle the user has. What I want to do is on the edit form, all 3 check boxes should be displayed regardless of how many check boxes were "checked" initially. So If the user selected 2 of the 3 boxes, all 3 should be shown but only those 2 are checked. I've actually got some of the code but there is a small problem in there. I am getting multiple check boxes. So if 1 check box was checked, that works fine, I get 3 check boxes, with the correct one ticked. But for 2 boxes initially ticked, I get 6 boxes and for 3 boxes inititally ticked I get 9 boxes. Here is my code: <?php if (!empty($VehSelection)) { foreach($table as $key => $value) { $VehSelection= $value["VehSelection"]; ?> <?php if ($VehSelection == 'Yacht') { ?> <input type="checkbox" id="yacht" name="vehicle[]" value="Yacht" checked/> <label for="yacht" class="boxstyle"> I have a yacht</label> <br><?php } else { ?> <input type="checkbox" id="yacht" name="vehicle[]" value="Yacht"> <label for="yacht" class="boxstyle"> I have a yacht</label><br> <?php } ?> <?php if ($VehSelection == 'SuperCar') { ?> <input type="checkbox" id="superCar" name="vehicle[]" value="SuperCar" checked/> <label for="superCar" class="boxstyle"> I have a super car</label><br> <?php } else { ?> <input type="checkbox" id="superCar" name="vehicle[]" value="SuperCar"> <label for="superCar" class="boxstyle"> I have a super car</label><br> <?php } ?> <?php if ($VehSelection == 'Plane') { ?> <input type="checkbox" id="plane" name="vehicle[]" value="Plane" checked/> <label for="plane" class="boxstyle"> I have a plane</label> <br><?php } else { ?> <input type="checkbox" id="plane" name="vehicle[]" value="Plane"> <label for="plane" class="boxstyle"> I have a plane</label></br> <?php } ?> <?php }} ?> Thank you
-
Hey Thanks, I ran that code but I get: Undefined index: fname all the way through each field up to and including fav_language I took your points on board and i've come up with this: <?php include ("db/connect.php"); include ('includes/error.php'); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $FormId = isset($_GET['user_id']) ? $_GET['user_id'] : ""; $sqlid = "SELECT * FROM Form WHERE FormId = '$FormId'"; $resultid = mysqli_query($link, $sqlid); if($resultid->num_rows == 0) { echo ("User does not exist"); exit(); } else { echo ("Found User!"); } $queryselect = "SELECT FormId, FirstName, LastName, Email, Age, Birthdate, FavLanguage FROM Form WHERE FormId = '$FormId'"; $result = mysqli_query($link, $queryselect); if (!$result) { printf("Error in connection: %s\n", mysqli_error($link)); exit(); } $table = []; while ( $row = mysqli_fetch_assoc( $result ) ) { $table[] = $row; } if ( count($table) != 1) { exit; } else { print_r($table); $first_name = $table[0]["FirstName"]; $last_name = $table[0]["LastName"]; $email = $table[0]["Email"]; $age = $table[0]["Age"]; $birthday = $table[0]["Birthdate"]; $favlanguage = $table[0]["FavLanguage"]; } if ($_SERVER['REQUEST_METHOD'] == 'POST') { if(!empty($_POST['fname'])){ $fname=$_POST['fname']; } if(!empty($_POST['lname'])){ $lname=$_POST['lname']; } if(!empty($_POST['email'])){ $email1=$_POST['email']; } if(!empty($_POST['age'])){ $age1=$_POST['age']; } if(!empty($_POST['birthday'])){ $birthday1=$_POST['birthday']; } if(!empty($_POST['fav_language'])){ $fav_language1=$_POST['fav_language']; } $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE FormId = '$FormId'"; $result1 = mysqli_query( $link, $updatequery ); echo ("update query after posing is: $updatequery </br>"); } echo ("update query is: $updatequery </br>"); echo ("id is: $FormId </br>"); ?> But everytime I hit submit, it comes up with User not Found, even though that user exists
-
Ok, ok, here is the complete code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> Document </title> </head> <body> <?php include ("db/connect.php"); include ('includes/error.php'); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $FormId = isset($_GET['user_id']) ? $_GET['user_id'] : ""; $queryselect = "SELECT FormId, FirstName, LastName, Email, Age, Birthdate, FavLanguage FROM Form WHERE FormId = '$FormId'"; $result = mysqli_query($link, $queryselect); if (!$result) { printf("Error in connection: %s\n", mysqli_error($link)); exit(); } $table = []; while ( $row = mysqli_fetch_assoc( $result ) ) { $table[] = $row; } if ( count($table) != 1) { exit; } else { print_r($table); $first_name = $table[0]["FirstName"]; $last_name = $table[0]["LastName"]; $email = $table[0]["Email"]; $age = $table[0]["Age"]; $birthday = $table[0]["Birthdate"]; $favlanguage = $table[0]["FavLanguage"]; } if(!empty($_POST['Submit'])) { $fname=""; $lname=""; $email1=""; $age1=""; $birthday1=""; $fav_language1=""; if(isset($_POST['fname'])){ $fname=$_POST['fname']; } if(isset($_POST['lname'])){ $lname=$_POST['lname']; } if(isset($_POST['email'])){ $email1=$_POST['email']; } if(isset($_POST['age'])){ $age1=$_POST['age']; } if(isset($_POST['birthday'])){ $birthday1=$_POST['birthday']; } if(isset($_POST['fav_language'])){ $fav_language1=$_POST['fav_language']; } $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE FormId = '$FormId'"; echo ("update query after posing is: $updatequery </br>"); $result1 = mysqli_query( $link, $updatequery ); } echo ("update query is: $updatequery </br>"); echo ("id is: $FormId </br>"); ?> <h2>Update the entry</h2> <form name="funform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="<?php echo($first_name); ?>"><span id="errorfname"></span><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="<?php echo($last_name); ?>"><span id="errorlname"></span><br> <label for="email">Email:</label><br> <input type="text" id="email" name="email" value="<?php echo($email); ?>"><span id="erroremail"></span> <span id="errorpattern"></span><br> <label for="age">Age:</label><br> <input type="number" id="age" name="age" value="<?php echo($age); ?>"><span id="errorage"></span><br> <label for="birthday">Birthday:</label><br> <input type="date" id="birthday" name="birthday" value="<?php echo($birthday); ?>"><span id="errorbday"></span><br> <p>Choose your favorite Web language:</p> <input type="radio" id="html" name="fav_language" value="<?php echo($favlanguage); ?>"> <label for="html" class="radiostyle">HTML</label><br> <input type="radio" id="css" name="fav_language" value="<?php echo($favlanguage); ?>"> <label for="css" class="radiostyle">CSS</label><br> <input type="radio" id="javascript" name="fav_language" value="<?php echo($favlanguage); ?>"> <label for="javascript" class="radiostyle">JavaScript</label><br><br><br> <span id="errorlang"></span><br> <?php mysqli_close($link); ?> </body> </html>
-
Hi Guys, I've been trying to put everything related to validating the form and updating the database within: if(!empty($_POST['Submit'])) { ... } My brackets seem to match up, but it is still not updating (although some progress has been made as I am not being "blank rowed" anymore). Now I am getting a : Undefined variable: updatequery error, on the line where I echo $updatequery. This must mean that my UPDATE statement variable is not even been set. Can anyone see where I'm going wrong please? <?php if(!empty($_POST['Submit'])) { $fname=""; $lname=""; $email1=""; $age1=""; $birthday1=""; $fav_language1=""; if(isset($_POST['fname'])){ $fname=$_POST['fname']; } if(isset($_POST['lname'])){ $lname=$_POST['lname']; } if(isset($_POST['email'])){ $email1=$_POST['email']; } if(isset($_POST['age'])){ $age1=$_POST['age']; } if(isset($_POST['birthday'])){ $birthday1=$_POST['birthday']; } if(isset($_POST['fav_language'])){ $fav_language1=$_POST['fav_language']; } $updatequery = "UPDATE Form SET FirstName = '$fname', LastName = '$lname', email = '$email1', age = '$age1', Birthdate= '$birthday1', FavLanguage = '$fav_language1' WHERE FormId = '$FormId'"; $result1 = mysqli_query( $link, $updatequery ); } echo ("update query is: $updatequery </br>"); echo ("id is: $FormId </br>"); ?>