Here is the page show table from database and every row has textbox, and b_id to update table.
$result2 = mysqli_query($con,"SELECT * FROM zahialga WHERE bid IN ($imp)");
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['b_id'] . "</td>";
echo "<td>" . $row['materials'] . "</td>";
echo "<td>" . $row['num'] . "</td>";
echo "<td> <input type='text' name='sh_num[]' maxlength='10'></td>";
echo "</tr>";
echo "<input type='hidden' name='b_id[]' value='" . $row['bid'] . "'>";
}
echo "</table>";
echo "<input type='submit' value='Илгээх'/>";
this is update page.
$con=mysqli_connect("localhost","root","","login");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sh_id_query = mysqli_query($con,"SELECT sh_id FROM zahialga order by bid desc limit 1");
$sh_id1 = $sh_id_query->fetch_object()->sh_id;
$sh_id2 = $sh_id1 + 1;
$count = count(array_filter($_POST["sh_num"]));
for($i=0;$i<$count;$i++)
{
$insert = mysqli_query($con,"UPDATE table SET
sh_id='{$sh_id2}',
sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['$b_id'][$i]}'");
}
this is getiing Notice: Undefined index: $b_id error. What im missed?
BTW sorry about my bad english.
There should be no $ sign:
$_POST['b_id'][$i]
$insert = mysqli_query($con,"UPDATE table SET
sh_id='{$sh_id2}',
sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['$b_id'][$i]}'");
// ^ additional $ sign here
should be
$insert = mysqli_query($con,"UPDATE table SET
sh_id='{$sh_id2}',
sh_num='{$_POST['sh_num'][$i]}'
WHERE bid = '{$_POST['b_id'][$i]}'");
Related
I am trying to create a basic advanced search with an input which will then search through any results that have a matching category field that is selected in the dropdown and then also a matching keyword field for company_name in "advancedSearch". I have gotten to the stage where I can use the drop down to then display the matching data but I’m having trouble querying that with the search input.
Here is my form code from index.php
<form action="advanced-search.php" method="POST">
<input id="advancedInput" placeholder="Advanced Search" type="search" name="advancedSearch">
<?php
$sqlSelect="SELECT category FROM categories";
$result = $db -> query ($sqlSelect);
echo "<select id=\"selectAdvanced\" name=\"value\">";
echo "<option></option>";
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
foreach ($rows as $row) {
print "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>";
}
echo "</select>";
?>
<input type="submit" value="search"/>
</form>
And here is the code from my advanced-search.php
<?php
if(isset($_POST['value']) && !empty($_POST['value'])) {
$username = trim(strip_tags($_POST['value']));
include('dbConfig.php');
if (mysqli_connect_errno()) {
printf("Can't connect: %s\n", mysqli_connect_error());
exit();
}
$where = ($username == "category")? "" : " WHERE category = '$username'";
$sql = "SELECT * FROM company_listings" . $where; // Create Query
$result = mysqli_query($db, $sql); // Run Query
echo "<table border=1><tr><th>id</th><th>name</th><th>created</th></tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
This code works great for echoing out the matching categories from the dropdown but I cant work out how I would further query the search from the "advanced search" input.
Any help would be greatly appreciated.
I worked out that using AND in my query like this and using the Request on submit I was able to get what I was after... Thanks to #ADyson for the nudge. I have added my updated advanced-search.php file below;
if(isset($_REQUEST['submit'])){
$username = (($_POST['value']));
$advanced = (($_POST['advancedSearch']));
include('dbConfig.php');
if (mysqli_connect_errno()) {
printf("Can't connect: %s\n", mysqli_connect_error());
exit();
}
$sql=" SELECT * FROM company_listings WHERE company_name like '%".$advanced."%' AND category LIKE '%".$username."%'";
$result = mysqli_query($db, $sql); // Run Query
echo "<table border=1><tr><th>id</th><th>name</th><th>created</th></tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.
Sorry if my english is not correct. I will try my best..
The question is how can I insert record from existing table with insert button. Everything work fine except variable $book. It records value 0 to database. Here is code..
//if the user press "INSERT BOOK" button there will be new record in database
<?php
$reader=$_SESSION['user_id'];
$book=$row['book_id'];
if (isset($_POST['update'])){
$sql = "INSERT INTO `read` (`read_id`, `book`, `reader`) VALUES (LAST_INSERT_ID(), '$book' '$reader')";
$result = mysql_query($sql) or die(mysql_error());
if ($result) {
echo " Yes, it works!";
}
else{
echo "noup!";
}
}
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
$result = mysql_query("SELECT * FROM book b JOIN `read` r ON (b.book_id = r.book) JOIN users u ON (r.reader = u.user_id) WHERE b.book_name LIKE '%" . $name . "%' or b.writer LIKE '%" . $name . "%' AND u.user_id !=". $_SESSION['user_id']);
$num_rows = mysql_num_rows($result);
if($num_rows>=1){
echo "<table id='table1'>
<tr>
<th>book_id</th>
<th>BOOKNAME</th>
<th>WRITER</th>
<th>PAGES</th>
</tr>";
while($row=mysql_fetch_array($result)){
echo "<form action=add_book.php method='post'>";
echo "<tr>";
echo "<td>" . $row['book_id'] . "</td>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['writer'] . "</td>";
echo "<td>" . $row['pages'] . "</td>";
//echo "<td>" . "<input type = 'hidden' name = 'hidden' value =" . $row['book_id'] . " </td>";
echo "<td>" . "<input class = 'field' type = 'submit' name = 'update' value = 'INSERT BOOK'>" . " </td>";
echo "</tr>";
echo "</form>";
}//WHILE LOOP
echo "</table>";
}
else{
echo "NO RESULTS";
}//else
}//if(preg_match..)
} //if(isset($_GET['go'])){
} //if(isset($_POST['submit'])){
?>
I have this code of a table here:
while($record=mysql_fetch_array($myData) ) {
echo "<form action=mydata3.php method=post>";
echo"<tr>";
echo "<td>" . $record['Id'] . "</td>";
echo "<td>" . $record['Name'] . "</td>";
echo "<td>" . $record['Surname'] . "</td>";
echo "<td align='center'>" . "<input type=checkbox name=attendance value=". $record['Attendance'] . " </td>";
echo "</form>";
echo"<tr>";
}
I would like I click the attendance button to update the records of the sql database.
If the checkbox of each student is checked the the value of attend should be Yes if is not checked it should be No. I managed to do something but is getting the checkbox value of the first student and it gives it to all the students.
Function:
if (isset($_POST['updatesec'])) {
$sql = " SELECT * FROM students";
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die("Cannot connect: " . mysql_error());
}
$myData = mysql_query($sql, $con);
while ($students = mysql_fetch_array($myData)) {
$UpdateQuery = "UPDATE students SET Attendance=' " . check() . " ' WHERE Id=$students[Id]";
mysql_query($UpdateQuery, $con);
}
}
Function check() {
if (isset($_POST['attendance'])) {
return 'yes';
} else {
return 'no';
}
}
update: I have change the code as follows
while($record=mysql_fetch_array($myData) ) {
echo "<form action=studentstable.php method=post>";
echo"<tr>";
echo "<td>" . $record['Id'] . "</td>";
echo "<td>" . $record['Name'] . "</td>";
echo "<td>" . $record['Surname'] . "</td>";
echo "<td align='center'>" . "<input type=checkbox name='attendance[".$record['Id']."]' value='". $record['Attendance'] . "' /> </td>";
echo "</form>";
echo"<tr>";
}
if (isset($_POST['updatesec'])) {
$myData = mysql_query( $sql,$con);
while ($students = mysql_fetch_array($myData)) {
$UpdateQuery = "UPDATE students SET Attendance= '" . check($students['Id']) . " ' WHERE id=$students[Id]";
mysql_query($UpdateQuery, $con);
}
}
Function check($id) {
if (isset($_POST['attendance'][$id])) {
return 'yes';
} else {
return 'no';
}
}
But is still only working for the first check box of the table
U need to change the name of your checboxes and your check function:
function check($id) {
if (isset($_POST['attendance'][$id])) {
return 'yes';
} else {
return 'no';
}
}
echo "<td align='center'>" . "<input type=checkbox name='attendance[".$record['Id']."]' value='". $record['Attendance'] . "' /> </td>";
This way will u have an array of all the checkboxes with a link to the id of the attendant.
while ($students = mysql_fetch_array($myData)) {
$UpdateQuery = "UPDATE students SET Attendance=' " . check($students['Id']) . " ' WHERE Id=$students[Id]";
mysql_query($UpdateQuery, $con);
}
On a sidenote. mysql_* functions are deprecated. U should move on to mysqli or PDO and start using prepared statements
Error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\webareas\ie803\projectx\search.php on line 306
My php code is as follows:
<?php
$car = mysql_real_escape_string($_REQUEST['car']);
$model = mysql_real_escape_string($_REQUEST['model']);
$type = mysql_real_escape_string($_REQUEST['type']);
$colour = mysql_real_escape_string($_REQUEST['colour']);
$year = mysql_real_escape_string($_REQUEST['year']);
$price = mysql_real_escape_string($_REQUEST['price']);
$con = mysql_connect("--","---","---");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('-----', $con);
$sql = "SELECT * FROM Cars WHERE Make ='$car', Model ='$model', Type ='$type', Colour = '$colour', Year = '$year', Price = '$price'";
$result=mysql_query($sql, $con);
{
while($info = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $info['Make']. "</td>";
echo "<td>" . $info['Model']. "</td>";
echo "<td>" . $info['Type']. "</td>";
echo "<td>" . $info['Colour']. "</td>";
echo "<td>" . $info['Year']. "</td>";
echo "<td>" . $info['Price']. "</td>";
echo "<br/><br/><td>" . '<hr>' . "</td>";
}
}
echo "</tr>";
echo "</table>";
?>
line 306 is the while statement. I keep on getting errors after each change I make.
The select statement is just a string, you have to feed it through mysql_query to get the result set, it is typically done as follows:
$sql = "SELECT * FROM Cars WHERE Make ='$car', Model ='$model', Type ='$type', Colour = '$colour', Year = '$year', Price = '$price'";
$result=mysql_query($sql, $con);
You need to use mysql_query before mysql-fetch-aarray. See this page for example of how to do this
I don't think that your query is even being executed. I've never seen the syntax of WHERE [column], [column], [column] before.
Try:
SELECT * FROM Cars WHERE Make ='$car' AND Model ='$model' AND Type ='$type' AND Colour = '$colour' AND Year = '$year' AND Price = '$price'
I've added some error checking so that you can see whatever mysql errors you get, if the syntax was correct.
$sql = "SELECT * FROM Cars WHERE Make ='$car', Model ='$model', Type ='$type', Colour = '$colour', Year = '$year', Price = '$price'";
$result=mysql_query($sql, $con);
if($result)
{
while($info = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $info['Make']. "</td>";
echo "<td>" . $info['Model']. "</td>";
echo "<td>" . $info['Type']. "</td>";
echo "<td>" . $info['Colour']. "</td>";
echo "<td>" . $info['Year']. "</td>";
echo "<td>" . $info['Price']. "</td>";
echo "<br/><br/><td>" . '<hr>' . "</td>";
}
else {
die(mysql_error();
}
You don't have mysql_query(), try
$sql = "SELECT * FROM Cars WHERE Make ='$car', Model ='$model', Type ='$type', Colour = '$colour', Year = '$year', Price = '$price'";
$result = mysql_query($sql) or die("ERROR: ".mysql_error());
EDIT
I put in the or die statement to help with debug, you can remove it at any time you wish. I personally find it makes it easier.