Jump to content

ciresuark

Members
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

0 Neutral

About ciresuark

  • Rank
    Member
  1. After some playing and talking it out loud to myself I was able to figure it out. The code block should be like below.I had a double quote in the wrong spot, and the statement off a little bit. foreach ($countries->fetchAll() as $country): ?> <option value="<?php echo $country['countryName']; ?>" <?php echo isset($selectedCountry) && $selectedCountry['countryName'] == $country['countryName'] ? ' selected':''?> ><?php echo $country['countryName']; ?></option> <?php endforeach; Thank you for the help.
  2. Agreed. Right now as I look at this I think the primary issue is I am not reading the input from the "customer". The start of the foreach loop looks at countries which is: SELECT * From countries (sql2), which lists all the countries available within the db and makes them available within the combobox. <?php foreach ($countries->fetchAll() as $country): ?> <option value="<?php echo $country['countryName']; ?> <?php echo isset($selectedCountry) == $selectedCountry['countryName'] ? ' selected':''?> "><?php echo $country['countryName']; ?></option> <?php endforeach; ?> As I am looking through this I think the code line below is my issue. As I'm looking at the selected country twice and not gathering what the country should be to verify it is correct. Does that sound right? <?php echo isset($selectedCountry) == $selectedCountry['countryName'] ? ' selected':''?> In non-php I want to check the selected country from the db against the customerID's country and highlight it. $countryQuery = " {$sql2} INNER JOIN customers ON countries.countryCode = customers.countryCode WHERE customers.customerID = $customerID"; $countriesQuery = $db->prepare($countryQuery); $countriesQuery->execute(['customerID' => $_GET['customerID']]); $selectedCountry = $countriesQuery->fetch(PDO::FETCH_ASSOC);
  3. I'm not sure why I did it that way. I changed it to countryName. The countries table only has countryCode and CountryName. There are no ids. After I changed it to <option value="<?php echo $country['countryName']; ?> the error went away. This is what the inspected element looks like. <option value="Sao Tome and Princip selected ">Sao Tome and Princip</option> If I am seeing this right, I need to move the " to have the selected outside the quote. Right now all options are showing as selected.
  4. I did what you mentioned and I am receiving an Undefined index: customer ID on line 94.
  5. I don't know if it makes it more difficult, but the countries are stored in a different table with the country abbreviation which is why I have the join. In the results you show, are you able to see the other options if a user needed to make updates?
  6. Yes, there are no errors in the code. The top of the page has the var_dump: array(12) { ["countryCode"]=> string(2) "US" ["countryName"]=> string(13) "United States" ["customerID"]=> string(4) "1045" ["firstName"]=> string(9) "Priscilla" ["lastName"]=> string(5) "Smith" ["address"]=> string(8) "Box 1979" ["city"]=> string(6) "Marion" ["state"]=> string(2) "OH" ["postalCode"]=> string(5) "43305" ["phone"]=> string(14) "(800) 555-1669" ["email"]=> string(18) "psmith@example.com" ["password"]=> string(6) "sesame" } I am tried to use this to determine if my query was pulling the information. The section pulling this is: <form action="update.php" method="get"> <select name="country"> <option value=""></option> <?php foreach ($countries->fetchAll() as $country): ?> <option value="<?php echo $country['customerID']; ?> <?php echo isset($customerID) == $selectedCountry['customerID'] ? ' selected':''?> "><?php echo $country['countryName']; ?></option> <?php endforeach;?> </select> </form> and I think I need to update: <?php echo isset($customerID) == $selectedCountry['customerID'] ? ' selected':''?> It isn't right as the combo box isn't highlighting "United States".
  7. I have a form gathering data from the database. There is one field 'country' which is a combo box. The combo box is populated from the countries table which joins the customers table to provide the country specific to the customer. I'm trying to have the combo box display the country associated with the customer. I think I'm close in my code, but unsure. This is a playground to so I can implement on our other site. <?php require_once('database.php'); $sql2 = "SELECT * From countries "; $countries = $db->query($sql2); if(isset($_GET['customerID'])) { $customerID = filter_input(INPUT_GET, 'customerID', FILTER_SANITIZE_NUMBER_INT); $sql = "SELECT * FROM customers WHERE customerID =$customerID "; //$sql2 = "SELECT * From countries //INNER JOIN customers ON countries.countryCode=customers.countryCode //WHERE customers.customerID = $customerID"; $stmt = $db->query($sql); } if(isset($_GET['customerID'])){ $customerID = filter_input(INPUT_GET, 'customerID', FILTER_SANITIZE_NUMBER_INT); $countryQuery = " {$sql2} INNER JOIN customers ON countries.countryCode = customers.countryCode WHERE customers.customerID = $customerID"; $countriesQuery = $db->prepare($countryQuery); $countriesQuery->execute(['customerID' => $_GET['customerID']]); $selectedCountry = $countriesQuery->fetch(PDO::FETCH_ASSOC); var_dump($selectedCountry); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>My Guitar Shop</title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <!-- the body section --> <body> <div id="page"> <div id="header"> <h1>SportsPro Technical Support</h1> <p>Sports management software for the sports enthusiast.</p></h1> </div> <div id="main"> <h1>View/Update Customer</h1> <form action="update.php" method="get" > <?php ?> <div id="content"> <!-- display a table of products --> <h2>Customers</h2> <form name="customerInfo"> <?php foreach ($stmt as $cust) { ?> <div> <label>First Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['firstName']; ?>"> </div><br> <div> <label>Last Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['lastName']; ?>"> </div><br> <div> <label>Address</label> <input type="text" name="address" class ="form-control" value ="<?php echo $cust['address']; ?>"> </div><br> <div> <label>City</label> <input type="text" name="city" class ="form-control" value ="<?php echo $cust['city']; ?>"> </div><br> <div> <label>State</label> <input type="text" name="state" class ="form-control" value ="<?php echo $cust['state']; ?>"> </div><br> <form action="update.php" method="get"> <select name="country"> <option value=""></option> <?php foreach ($countries->fetchAll() as $country): ?> <option value="<?php echo $country['customerID']; ?> <?php echo isset($customerID) == $selectedCountry['customerID'] ? ' selected':''?> "><?php echo $country['countryName']; ?></option> <?php endforeach;?> </select> </form> </div> <br> <div> <label>Country Code</label> <input type="text" name="countryCode" class ="form-control" value ="<?php echo $cust['countryCode']; ?>"> </div><br> <div> <label>Zip Code</label> <input type="text" name="postalCode" class ="form-control" value ="<?php echo $cust['postalCode']; ?>"> </div><br> <div> <label>Email </label> <input type="text" name="email" class ="form-control" value ="<?php echo $cust['email']; ?>"> </div><br> <div> <label>Phone Number </label> <input type="text" name="phone" class ="form-control" value ="<?php echo $cust['phone']; ?>"> </div><br> <div> <label>Password </label> <input type="text" name="password" class ="form-control" value ="<?php echo $cust['password']; ?>"> </div><br> <div> <?php } ?> </div> </div> <form action="UpdateCustomer.php" method="get"> <input type="submit" name="data" value="Update_Data"></input> </form> <div id="footer"> <p> © <?php echo date("Y"); ?> SportsPro, Inc. </p> </div> </div><!-- end page --> </body> </html>
  8. I'm new to PHP and I was able to figure out how to populate data from my database into my text fields. I am trying to add the update information to the same php file; however, I am now receiving errors within the data I was able to populate, Notice: Undefined variable: stmt in C:\xampp\htdocs\Cust_App\update.php on line 47 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Cust_App\update.php on line 47 and errors with the Update statement Notice: Undefined variable: stmtupdate in C:\xampp\htdocs\Cust_App\update.php on line 96 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\Cust_App\update.php on line 96 Notice: Undefined variable: customerID in C:\xampp\htdocs\Cust_App\update.php on line 96 I had defined the customerID variable above in the code, but it isn't being captured from here. I tried setting up this query like the one which gathered the data, but I'm off by a little bit. I would like the option to be able to update all fields. Any help is appreciated. I'm trying to learn as I may get asked to update other forms in the future. (new boss asks a lot) <?php require_once('database.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- the head section --> <head> <title>My Guitar Shop</title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <!-- the body section --> <body> <div id="page"> <div id="header"> <h1>SportsPro Technical Support</h1> <p>Sports management software for the sports enthusiast.</p></h1> </div> <div id="main"> <h1>View/Update Customer</h1> <form action="update.php" method="get" > <?php if(isset($_GET['customerID'])) { $customerID = filter_input(INPUT_GET, 'customerID', FILTER_SANITIZE_NUMBER_INT); $sql = "SELECT * FROM customers WHERE customerID =$customerID "; $stmt = $db->query($sql); } ?> <div id="content"> <!-- display a table of products --> <h2>Customers</h2> <form method = "edit"> <?php foreach ($stmt as $cust) { ?> <div> <label>First Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['firstName']; ?>"> </div><br> <div> <label>Last Name</label> <input type="text" name="name" class ="form-control" value ="<?php echo $cust['lastName']; ?>"> </div><br> <div> <label>Address</label> <input type="text" name="address" class ="form-control" value ="<?php echo $cust['address']; ?>"> </div><br> <div> <label>City</label> <input type="text" name="city" class ="form-control" value ="<?php echo $cust['city']; ?>"> </div><br> <div> <label>State</label> <input type="text" name="state" class ="form-control" value ="<?php echo $cust['state']; ?>"> </div><br> <div> <label>Country</label> <input type="text" name="countryCode" class ="form-control" value ="<?php echo $cust['countryCode']; ?>"> </div><br> <div> <label>Zip Code</label> <input type="text" name="postalCode" class ="form-control" value ="<?php echo $cust['postalCode']; ?>"> </div><br> <div> <label>Email </label> <input type="text" name="email" class ="form-control" value ="<?php echo $cust['email']; ?>"> </div><br> <div> <label>Phone Number </label> <input type="text" name="phone" class ="form-control" value ="<?php echo $cust['phone']; ?>"> </div><br> <div> <label>Password </label> <input type="text" name="password" class ="form-control" value ="<?php echo $cust['password']; ?>"> </div><br> <div> <?php } ?> <input type="Submit" name="Update_Data" value="Update Data"></input> <?php $sql2 = "UPDATE customers SET firstName = ". $stmtupdate['firstName']." WHERE customerID =$customerID "; $stmtupdate = $db->query($sql2); ?> </div> </div> <div id="footer"> <p> &copy; <?php echo date("Y"); ?> SportsPro, Inc. </p> </div> </div><!-- end page --> </body> </html>
  9. My apologies. The primary connection is made in the mysql_connect.php file. <?php // Create a connection to the logindb database. // Set the encoding and the access details as constants: Define ('DB_USER', '***'); Define ('DB_PASSWORD', '***'); Define ('DB_HOST', '***'); Define ('DB_NAME', '***'); // Make the connection: $dbcon = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); mysqli_set_charset($dbcon, 'utf8'); ?> I'm looking at the manual (https://www.php.net/mysqli_error). Were you pulling this information from another section? I would like to learn, and find resources to check first prior to posting. Just trying to get my bearings. I just started PHP a few weeks ago.
  10. Here is the start of the script. mysqli_stmt_bind_param($q, 'ssss', $first_name, $last_name, $email, $hashed_passcode); // execute query mysqli_stmt_execute($q); if (mysqli_stmt_affected_rows($q) == 1) { // One record inserted There is a separate PHP file called out at the top of the original post: require ('msqli_connect.php'); Hope that answers the question.
  11. Hello, I have a registration page requesting simple information (first name, last name, email and password). When I click my 'register' button I receive the following errors: Warning: mysqli_stmt_bind_param(): invalid object or resource mysqli_stmt in C:\xampp\htdocs\login\process-register-page.php on line 45 Warning: mysqli_stmt_execute(): invalid object or resource mysqli_stmt in C:\xampp\htdocs\login\process-register-page.php on line 47 Warning: mysqli_stmt_affected_rows(): invalid object or resource mysqli_stmt in C:\xampp\htdocs\login\process-register-page.php on line 48 Below is most of the code where I experience issues. The lines called out are: mysqli_stmt_bind_param($q, 'ssss', $first_name, $last_name, $email, $hashed_passcode); // execute query mysqli_stmt_execute($q); if (mysqli_stmt_affected_rows($q) == 1) { // One record inserted if (empty($errors)) { // If everything's OK. // Register the user in the database... // Hash password current 60 characters but can increase $hashed_passcode = password_hash($password1, PASSWORD_DEFAULT); require ('msqli_connect.php'); // Connect to the db. // Make the query: $query = "INSERT INTO users (userid, first_name, last_name, "; $query .= "email, password, registration_date) "; $query .="VALUES(' ', ?, ?, ?, ?, NOW() )"; $q = mysqli_stmt_init($dbcon); mysqli_stmt_prepare($q, $query); // use prepared statement to ensure that only text is inserted // bind fields to SQL Statement mysqli_stmt_bind_param($q, 'ssss', $first_name, $last_name, $email, $hashed_passcode); // execute query mysqli_stmt_execute($q); if (mysqli_stmt_affected_rows($q) == 1) { // One record inserted header ("location: register-thanks.php"); exit(); } else { // If it did not run OK. // Public message: $errorstring = "<p class='text-center col-sm-8' style='color:red'>"; $errorstring .= "System Error<br />You could not be registered due "; $errorstring .= "to a system error. We apologize for any inconvenience.</p>"; echo "<p class=' text-center col-sm-2' style='color:red'>$errorstring</p>"; // Debugging message below do not use in production //echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $query . '</p>'; mysqli_close($dbcon); // Close the database connection. // include footer then close program to stop execution echo '<footer class="jumbotron text-center col-sm-12" style="padding-bottom:1px; padding-top:8px;"> include("footer.php"); </footer>'; exit(); } } else { // Report the errors. $errorstring = "Error! <br /> The following error(s) occurred:<br>"; foreach ($errors as $msg) { // Print each error. $errorstring .= " - $msg<br>\n"; } $errorstring .= "Please try again.<br>"; echo "<p class=' text-center col-sm-2' style='color:red'>$errorstring</p>"; }// End of if (empty($errors)) IF. } catch(Exception $e) // We finally handle any problems here { // print "An Exception occurred. Message: " . $e->getMessage(); print "The system is busy please try later"; } catch(Error $e) { //print "An Error occurred. Message: " . $e->getMessage(); print "The system is busy please try again later."; } ?> I have done some searching around to try and figure out the issue; but I can't seem to put my finger on it. This is a new database, and I haven't been able to get test registered as of yet. Any help would be appreciated. Thank you
  12. Hi everyone. I am in school to become a software developer. I am taking a PHP class this semester and hope to find some good information on here. Thanks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.