PHP - Mysql_num_rows And Select Menu
Could someone help me I really dont know how to go about coding this, so i would be happy if someone could point me in the right way
Well what I am trying to do is use mysql_num_rows to call up how many rows in the table. The using how many rows, use a menu with the numbers of rows that are in the table ex below mysql_num_rows gets 5 rows so menu is <select name="order" > <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> Similar TutorialsHi I want to add student from a dropdown list to database but I have some problem. This is my select dropdown menu code <form name ="student" method = "POST" action ="confirmation.php"> <select name="name"> <option selected>Select Student</option> <?php $arrStudent = executeSelectQuery("select * FROM user "); for ($i = 0; $i < count($arrStudent); $i++) { $student_result = $arrStudent[$i]['student_id']; $name_result = $arrStudent[$i]['student_name']; ?> <option value="<?php echo $id_result; ?>"><?php echo $id_result; ?>, <?php echo $name_result; ?></option> <?php } ?> </select> </form> The output in the dropdown menu look something like this: 1, Alvin 2, Benny 3, Charles 4, Daniel 5, Eva and so on... After submitting the form, it will proceed to confirmation.php page. At the confirmation page, I have the following variable: $student_result = $_REQUEST['student_id']; $name_result = $_REQUEST['student_name']; I want to insert to database with the following insert query $sql = "INSERT INTO student(student_id, student_name) VALUES ('". $student_result . "', '". $name_result ."')"; $insert = executeInsertQuery($sql); It can insert successfully but, it will not insert the student_name. May I know where I did wrongly? Thanks Ben Chew Hi, I'm no pro at PHP but I am trying to get a drop down menu to a authenticate before moving to the next part of the form. What I want is once a selection has been made, ONLY THEN can the user move on, OTHERWISE a message echo appears. This is the html menu box <select size="1" name="title"> <option>Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Dr">Dr</option> </select> Then this is what I have in the form PHP: $visitortitle = $_POST['visitortitle']; if ( HOW DO I GET THIS PART TO AUTHENTICATE AN OPTION HAS BEEN SELECTED? ) { echo "<p>Please enter a title correctly<br />before you try submitting the form again.</p>\n"; die ( '<a href="pef.html">click here go back and try again</a>' ); echo $id;} If anyone can help me sort out this part of the form I can move on as the rest is working fine? Thanks Gary Hello, I'm using a dynamically created select menu for a user to make a choice which will then be put in my database with the following code: <select> <?php foreach ($course_number as $row) { echo "<option value = '{$row['course_id']}'"; if ($errors && $_POST["course_id"] == $row['course_id']) {echo 'selected = "selected"'; } echo ">{$row['course_number']}</option>"; } ?> Unfortunately, I'm having some problems figuring out how to pull off the selected value. Right now my database portion looks like this: $data = array('assignment_name' => $_POST['assignment_name'], 'due_date' => $_POST['due_date'], 'course_id' => $_POST["row['course_id']"]); $inserted = $dbWrite->insert('assignments_instructors',$data) While the assignment_name and due_dates work (they come from text fields), my course_id gives me an Undefined index: row['course_id'] error. Any help would be appreciated. Thank you. I am using jquery .change function to perform an operation when a month is selected from a drop down menu. The change works but I am unable to update the value of the drop down menu with the updated month. My drop down shows the starting value as default even on change. Can anyone help. Following is the code snippet that does change and then the drop down menu form. Code: [Select] $("#monthName").change(function() { alert($("#monthName").val()); if ($("#post").val() == 1) { $("#monthselect").submit(); } }); Code: [Select] <form id="monthselect" action="<?=$_SERVER['PHP_SELF']?>" method="get"> <input id="post" type="hidden" name="post" value="1"> <label>SELECT MONTH</label> <select id="monthName" name="monthName"> <option value="January">January</option> <option value="February">February</option> <option value="March">March</option> <option value="April">April</option> <option value="May">May</option> <option value="June">June</option> <option value="July">July</option> <option value="August">August</option> <option value="September">September</option> <option value="October">October</option> <option value="November">November</option> <option value="December">December</option> </select> </form> Even after the change, January shows up by default even if I select say June or July. I tried something like following but did not work. Code: [Select] $("#monthName option[value=" + $("#monthName").val() +"]").attr("selected","selected") ; I have a test database, I have two names in the database which get returned just fine in my html dropdown. I am trying to figure out how to figure out which item in the list was selected so that I can return information from another table based on the selected id. This is what I am trying now but I don't know how to proceed or if it is correct Code: [Select] $result = $mysql->query("SELECT * FROM names") or die($mysql->error); ?> <select> <?php if($result){ while($row = $result->fetch_object()){ $id = $row->nameID; $name = $row->firstName . " " . $row->lastName; ?> <option value"<?php $id ?>"><?php echo $name ?></option> <?php }?> </select> <?php } ?> the same page? Hi there, i am relatively new to php, mysql, css etc but learning fast. My problem is such; i have a php file which is doing a SELECT mysql_query, WHILE results to strings, then ECHO the resulting rows to produce a list formatted using <table> and finally this <table> is inside a <form> which will POST the changes back to the specific database.tble.row. I wish to have a drop down menu within the <form><table> which will be populated from a separate database.table. I have accomplished the drop down menu outside the <?php ?> tags inside <form><table> which POSTS to a php file but my problem is to add the populating drop down menu inside <?php ?> an already ECHOing resulting rows from the sql query. i.e <?php blurb and stuff ?> <form><table><tr><td> <select name etc> <?php $result = mysql_query("SELECT * FROM tbl WHERE string = tble.rw ORDER BY column"); while($row=mysql_fetch_array($result)){ echo "<OPTION VALUE=".$row['column'].">".$row['column']."</OPTION>"; } ?> </select> WORKS!!!! but placing this inside <?php $x =mysql_query[select] while {strings = conditions; echo ("<form><table><tr><td> insert populated drop menu here </td> etc "); echo"";}?> doesnt work and just leaves the select drop menu blank Hoep you understand my problem. I do not think i can attached the population WHILE loop to a string and just insert the string to the form but maybe i am wrong. thanks in advance and if you go tthis far reading you must be on lots and lots of coffee zark hello all, I was hoping someone could help me figure out how to add sizes to my shopping cart. Right now the items add just fine. The site sells shirts and I'm having trouble displaying the sizes once selected and added to the cart. It will post the size selected, but if the customer tries to add another shirt the size will overwrite the last. Any help is appreciated. Heres how I add items to the cart... Code: [Select] session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'empty': if($cart) { unset($cart); } break; case 'add': if ($cart) { $cart .= ','.$_GET['product_id']; } else { $cart = $_GET['product_id']; } break; The form with the size selection and add to cart button.. Code: [Select] <form action="cart.php?action=add&product_id=<?php echo $row_rs_products['product_id'] ?>" method="POST" name="addcart" id="addcart"> <table width="300" border="0"> <tr> <td><label for="sizes"></label> <select name="product_size" id="product_size" title="<?php echo $row_rs_products['product_size']; ?>"> <?php do { ?> <?php } while ($row_rs_sizes = mysql_fetch_assoc($rs_sizes)); $rows = mysql_num_rows($rs_sizes); if($rows > 0) { mysql_data_seek($rs_sizes, 0); $row_rs_sizes = mysql_fetch_assoc($rs_sizes); } ?> </select></td> <td><input type="submit" name="submit" id="submit" value="add to cart"> function displaying the cart.. Code: [Select] function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; // start div $output[] = '<div id="cart_table">'; // $output[] = '<tr>'; $output[] = '<td><h4>Product</h4></td>'; $output[] = '<td><h4>Item No.</h4></td>'; $output[] = '<td><h4>Price</h4></td>'; $output[] = '<td><h4>Size</h4></td>'; $output[] = '<td><h4>Quantity</h4></td>'; $output[] = '<td><h4>Price Total</h4></td>'; $output[] = '<td><h4> </h4></td>'; $output[] = '</tr>'; //new row foreach ($contents as $product_id=>$qty) { $sql = 'SELECT * FROM products WHERE product_id = '.$product_id; $result = $db->query($sql); $row = $result->fetch(); extract($row); // $output[] = '<tr>'; $output[] = '<td><a href="product.php?product_id='.$product_id.'">'.$product_title.'</a></td>'; $output[] = '<td>'.$product_plu.'</td>'; $output[] = '<td>$'.$product_price.'</td>'; // //$output[] = '<td>'.$product_size.'</td>'; $output[] = '<td>'.$_POST['product_size'].'</td>'; // $output[] = '<td><input type="text" name="qty'.$product_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>X $'.($product_price * $qty).'</td>'; $total += $product_price * $qty; $output[] = '<td><a href="cart.php?action=delete&product_id='.$product_id.'" class="r">Remove</a> </td>'; $output[] = '</tr>'; //end div $output[] = '</div>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>'; $output[] = '<div class="float-right"><button type="submit">Update cart</button>'; $output[] = '</form>'; Hello All
From the begin i have the following code.
<?php $sql = "SELECT id,phonemodel FROM iphone"; $rows = $conn->sqlExec($sql); $nr_row = $conn->num_rows; $meniu ='<ul>'; if($nr_row>=0) { foreach($rows as $row) { $meniu .= '<li><a href="iphone.php?id='.$row['id'].'">'.$row['phonemodel'].'</a></li>'; } } $meniu .= '</ul>'; echo $meniu; if(isset($_GET['id'])) { $id = (int)$_GET['id']; $sql = "SELECT * FROM iphone WHERE id = $id"; $rows = $conn->sqlExec($sql); $nr_rows = $conn->num_rows; if($nr_rows>0) { foreach($rows as $row){ echo 'Name Tel: '.$row["phonemodel"].' Title : '.$row["titlereparation"].' Pret : '.$row["price"].' Message : '.$row["msj"].' ID : '.$row["id"].'<br />'; } } else { echo '0 Results'; } } ?>Basicaly i have a website for phone repairs.And i want to create in iphone.php a menu from DB and when i acces the menu with _GET variable, when i press Iphone 5s (iphone.php?id=id page) the code have to display to me,all reparations for iphone 5s. Until now,i succssed to create the Menu but the code keep add same line in the menu when i add for example a second reparation for 5s.And i don't know how to select all reparation for 5s and display them in a single link like above Iphone 5s (iphone.php?id=id page). Now the script working like this.Create a menu with all phone names. Iphone 5s Iphone 3 Iphone 3s Iphone 4 Iphone 5s Iphone 5s Shoud be like Iphone 5s Iphone 3 Iphone 3s Iphone 4 Other new devices.. And display the reparation in every link from list By ID.i try it to select by phone names,not working. Any body with any ideea please? Thx so much Howdy everyone, please i need help changing a php coded form from a checkbox to a select menu. Here's the form. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" > <table class="dtable2"> <tr><th colspan="5">Enter a domain name:</th></tr> <tr><td colspan="5"><center>www.<input name="domain" type="text" size="35" /></center></td></tr> <tr><th colspan="5">Select an extension:</th></tr> <tr> <?php $i = 0; foreach ($this->serverList as $value) { if ($value['check'] == true) $checked=" checked "; else $checked = " "; echo '<td><input type="checkbox" name="top_'.$value['top'].'"'.$checked.'/>.'.$value['top'].'</td>'; $i++; if ($i > 4) { $i = 0; echo '</tr><tr>'; } } ?> </tr> </table> <center><input type="submit" name="submitBtn" class="sbtn" value="Check" /></center> </form> <?php I'll really appreciate your help. Hi, Im not sure whether this is a PHP or MySQL question, but im trying to get a select menu to only display the number of options that are defined in the mysql database.
For example
The code I have that retrieves the $quantity from the database.
Lets say that
$quantity = 3
Now I would like the option menu to only display three options, like this:
<select name="quantity"> <option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select>but for example, if the $quantity was equal to 10 then I would like the menu to be displayed as such <select name="quantity"> <option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select>How is it possible to do this? I am completely at a loss here. Many Thanks aquaman Hi. Maybe a tricky question? How do I reflect the content of a column from a database table in a roll down select menu in the browser? Let's say that the content of the table column is: Anna Michael These names should be reflected in this select menu like this: <select name="friends"> <option value="Choose a name">Choose a name</option> <option value="Anna">Anna</option> <option value="Michael">Michael</option> So visitors can choose a name, and thereby turn it into a variable, for reuse in the database. Best regards Morris I am working on a project where I want a select form to display information from a MySQL table. The select values will be different sports (basketball,baseball,hockey,football) and the display will be various players from those sports. I have set up so far two tables in MySQL. One is called 'sports' and contains two columns. Once called 'category_id' and that is the primary key and auto increments. The other column is 'sports' and contains the various sports I mentioned. For my select menu I created the following code. <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "You did not successfully connect to the DB!" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Error connecting to the database test!"); ?> <html> <head>Display MySQL</head> <body> <form name="form2" id="form2"action="" > <select name="categoryID"> <?php $sql = "SELECT category_id, sport FROM sports ". "ORDER BY sport"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"".$row['category_id']."\">".$row['sport']."</option>\n "; } ?> </select> </form> </body> </html> this works great. I also created another table called 'players' which contains the fields 'player_id' which is the primary key and auto increments, category_id' which is the foreign key for the sports table, sport, first_name, last_name. The code I am using the query and display the desired result is as follows <html> <head> <title>Get MySQL Data</title> </head> <body> <?php #connect to MySQL $conn = @mysql_connect( "localhost","uname","pw") or die( "Err:Db" ); #select the specified database $rs = @mysql_SELECT_DB ("test", $conn ) or die ( "Err:Db"); #create the query $sql ="SELECT * FROM sports INNER JOIN players ON sports.category_id = players.category_id WHERE players.sport = 'Basketball'"; #execute the query $rs = mysql_query($sql,$conn); #write the data while( $row = mysql_fetch_array( $rs) ) { echo ("<table border='1'><tr><td>"); echo ("Caetegory ID: " . $row["category_id"] ); echo ("</td>"); echo ("<td>"); echo ( "Sport: " .$row["sport"]); echo ("</td>"); echo ("<td>"); echo ( "first_name: " .$row["first_name"]); echo ("</td>"); echo ("<td>"); echo ( "last_name: " .$row["last_name"]); echo ("</td>"); echo ("</tr></table>"); } ?> </body> </html> this also works fine. All I need to do is tie the two together so that when a particular sport is selected, the query will display below in a table. I know I need to change my WHERE clause to a variable. This is what I need help with. thanks Hi I am trying to create a login script using the code below Code: [Select] <?php session_start(); include_once('includes/connect.php'); $user=$_POST['username']; $password=$_POST['password']; //check if username and password exists $checkuser=mysql_query("SELECT * from `tbl_users` where username = '$user' and password=md5('$password')"); if(mysql_num_rows($checkuser) > 0) //login name was found //if they do return to home page with relevant include { $user_row = mysql_fetch_array($checkuser); $_SESSION['auth']="yes"; $_SESSION['logname'] = $user_row['forename']; $_SESSION['usertype'] = $user_row['usertype']; header("Location: index.php"); exit; } //if not display login form with message that says logon details are incorrects else { echo('Do something else'); } ?> However I get a message saying that mysql_num_rows is not a valid resource. Any ideas where I am going wrong? I'm trying to loop out 10 rows of comment, but this code only displays comment one time, how do I display all rows? $query = "SELECT * FROM ".$prefix."comments WHERE userto = '".$user."' ORDER BY comment DESC LIMIT 0, 10"; $result = mysql_query($query); $num = mysql_num_rows($result); //Loop out code $i=0; while ($i < $num) { $comments=@mysql_result($result,$i,"comment"); $i++; } Hello, I am having a problem.. I get this message " Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in... " whenever i am trying to work my log in script. The script is: Quote <?php $host="localhost"; $username="root"; $password="pass"; $db_name="webDB"; $table_name="webmembers"; mysql_connect($host, $username, $password) or die ('Unable to connect'); mysql_select_db($db_name) or die ('Error'); $uname= $_POST['uname']; $pword= $_POST['pword']; $urname= stripslashes($uname); $pword= stripslashes($pword); $uname= mysql_real_escape_string($uname); $pword= mysql_real_escape_string($pword); $sql= "SELECT * FROM '$table_name' WHERE username: '$uname' and password: '$pword'"; $result=mysql_query($sql); $count= mysql_num_rows($result); <----------- problem if ($count == 1) { session_register($uname); session_register($pword); header("location: loginsuccess.php"); } else { echo $result,$count; echo "Wrong Username and Password"; } mysql_close(); ?> and Quote "loginsuccess.php" <?php session_start(); if(!sesssion_is_registered($uname)) { header("location: mainpage.php"); } ?> <html> <head> </head> <body> Log in Successful </body> </html> why do i get this warning? what am i doing wrong? Is there any simpler way to create a log in page but being secure as well? Thank in advance. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource. if ($_POST['subscribe']) { $email_subscribe = $_POST['email']; if (mysql_num_rows(mysql_query("SELECT * FROM newsletter WHERE email=$email_subscribe"))) { mysql_query("UPDATE newsletter SET active=1 WHERE email=$email_subscribe") or die ('Error updating the database'); echo "Thank you for subscribing!"; echo "<meta http-equiv=\"refresh\" content=\"0;url=javascript:history.back();\">"; } else { mysql_query("INSERT INTO newsletter (`email`,`active`) VALUES ('$email_subscribe', '1')"); echo "Thank you for subscribing!"; echo "<meta http-equiv=\"refresh\" content=\"0;url=javascript:history.back();\">"; } } elseif ($_POST['unsubscribe'] != 0) { $email_unsubscribe = $_POST['email']; if (mysql_num_rows(mysql_query("SELECT * FROM newsletter WHERE email=$email_unsubscribe"))) { mysql_query("UPDATE newsletter SET active=0 WHERE email=$email_unsubscribe") or die ('Error updating the database'); echo "You have been unsubscribed."; echo "<meta http-equiv=\"refresh\" content=\"5;url=javascript:history.back();\">"; } else { echo "Email does not exist."; echo "<meta http-equiv=\"refresh\" content=\"5;url=javascript:history.back();\">"; } $sql = "SELECT current FROM LIBusersX WHERE UserKey = '$Key' AND K_Type = $type AND current > 0 "; // echo $sql . "<br>"; $query = mysql_query ($sql) or die ("E112-100A"); $total = mysql_num_rows($query) or die ("E112-101A");Hi I have a website with a few users. I set up a system so that some users (as a trial) can only log on a fixed number of times. I have a field called current that counts down to zero. However when it reaches zero I get my error E112-101A Why do I get an error? Surely I should just get $total = 0 I am currently creating a form and I want to populate a drop down selection menu with data from two fields in a form. For example, I want it to pull the first and last name fields from a database to populate names in a drop down menu in a form. I want the form to submit to the email address of the person selected in the drop down. Is this possible to do? The email is already a field in the record of the person in the database. Can anyone give me some pointers or advice on how I should go about setting up the "Select" box drop down? I am not sure how to code it to do what I am wanting. Any links to relevant help would be appreciated too. Thanks in advance! |