Im new to PHP MySQL. Im developing a songbook site.
I want to create tables alphabetically in separate pages for each alphabet.
When a user click an alphabet in the menu it will direct to this page site/publicsearch.php?browse=a
This is the database :
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I pull the above database table like this : It works fine.
<?php
$servername = "localhost";
$username = "xxxxxxx";
$password = "xxxxxxxx";
$db_name = "xxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";
exit();
}
// Attempt select query execution
$sql = "SELECT * FROM lyrics_a";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Title</th>";
echo "<th>artist</th>";
echo "<th>cateogry</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> " . $row['eng_title'] . " " . $row['tel_title'] . " </td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I need separate alphabetical tables like this :
Alphabet "a" table results `site/publicsearch.php?browse=a`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
Alphabet "b" table results `site/publicsearch.php?browse=b`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I tried the below code doesn't work.
<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxx";
$db_name = "xxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo "<br><br>";
echo $row['id'];
echo $row['title'];
echo $row['lyrics'];
echo $row['alphabet'];
}
?>
I'm newbie. Please help. Thank you.
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
replace with:
$alphabet = mysqli_real_escape_string($conn,$_GET['browse']);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`= '" . $alphabet . "' ";
You propose the following URL: publicsearch.php?browse=a
But in your script you are using the request parameter $_GET['alphabet']
In addition, you don't need a separate column alphabet in your database, you can use SQL's LIKE:
$letter = $_GET['browse'];
$sql = "SELECT * FROM `lyrics` WHERE title LIKE '{$letter}%'"
Related
I want to display the comment only once e.g (testsetest) with the related images (which has the same imagesid by connecting the two tables).
Example (that I want to achieve):
comment: fool with images: name1, name 2
Caption of the wrong output.
The database structure
posts:
| commentid | comment | iamgesid |
------------------------------------
| 1 | fool | 5557 |
| 2 | fool2 | 5585 |
------------------------------------
multiple_image:
| id | image | imagesid |
---------------------------
| 1 | name1 | 5557 |
| 2 | name2 | 5557 |
| 3 | name3 | 5585 |
---------------------------
This is my current code:
$sql = "SELECT image, posts.imagesid, multiple_image.imagesid, comment
FROM multiple_image JOIN posts ON (multiple_image.imagesid=posts.imagesid)";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['comment'];
$imgs= "<div id='img_div'><img width='' src='upload/".$row['image']."' ></div>";
echo $imgs;
}
}
You will need to control break the result and order by the commentid
Please see the updated code.
$sql = "SELECT
image,
posts.imagesid,
multiple_image.imagesid,
comment
FROM
multiple_image
JOIN posts ON (multiple_image.imagesid=posts.imagesid)
ORDER BY
commentid
";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
if ($result->num_rows > 0) {
// output data of each row
$comment = '';
while($row = $result->fetch_assoc()) {
if($comment != $row['comment']){
echo $row['comment'];
$comment = $row['comment'];
}
$imgs= "<div id='img_div'><img width='' src='upload/".$row['image']."' ></div>";
echo $imgs;
}
}
You can create a flag and check if it is 0. If it is 0 then you can show comment else you dont need to show comment. Check below code:
if ($result->num_rows > 0) {
// output data of each row
$loop = 0;
while ($row = $result->fetch_assoc()) {
if ($loop == 0) {
echo $row['comment'];
}
$imgs = "<div id='img_div'><img width='' src='upload/" . $row['image'] . "' ></div>";
echo $imgs;
$loop++;
}
}
Hope it helps you.
I hope it will help out:
$sql = "SELECT
image,
posts.imagesid,
multiple_image.imagesid,
comment
FROM
multiple_image
JOIN posts ON (multiple_image.imagesid=posts.imagesid)
ORDER BY
commentid";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
if ($result->num_rows > 0) {
$comment = array();
while ($row = $result->fetch_assoc()) {
// check imagesid is not exist in array
if (in_array($row['imagesid'], $comment) == false) {
echo $row['comment'];
$comment[] = $row['imagesid'];
}
$imgs = "<div id='img_div'><img width='' src='upload/" . $row['image'] . "' ></div>";
echo $imgs;
}
}
I am connected to my database.
Created a drop down-box. Which works fine:
$sql = "SELECT * FROM Customer";
$result = mysql_query($sql);
echo "<b>Customer Name : </b>" . "<select id='CustomerName' name='CustomerName'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['CustomerName'] . "'>" . $row['CustomerName'] . "</option>";
}
Now I am stuck trying to put the values from table Customer into an array
Which looks like this:
$types = array (
'A' => 10.99,
'B' => 4.99,
'C' => 13.99);
What I intend to do is to create this array where,
Instead of A B C, there are CustomeName and CustomerPrice in place of the price.
The Customer table has
+----------------+-----------------+---------------+
| Customer_ID | CustomerName | CustomerPrice |
+----------------+-----------------+---------------+
| 1 | A | 10.99 |
| 2 | B | 4.99 |
| 3 | C | 13.99 |
+----------------+-----------------+---------------+
Please help
You basically do the same thing you did with the drop down except to generate the array. You could do it in the same loop:
$sql = "SELECT * FROM Customer";
$result = mysql_query($sql);
$array = array();
echo "<b>Customer Name : </b>" . "<select id='CustomerName' name='CustomerName'>";
while ($row = mysql_fetch_array($result)) {
$array[$row['CustomerName']] = $row['CustomerPrice'];
echo "<option value='" . $row['CustomerName'] . "'>" . $row['CustomerName'] . "</option>";
}
Hope this helps
I am trying write code in PHP with a Mysql database, and the problem is I want to show all rows with a same column value. for example like this:
id | Name | age | Location | type
----+----------+-----+----------+------
1 | Ane | 22 | SG | 1
2 | Angi | 19 | IND | 2
3 | Bobby | 23 | PH | 1
4 | Denis | 26 | IND | 1
5 | Jerry | 21 | SG | 1
6 | Mikha | 25 | JP | 2
I want only show the rows with value in column type = 1 or value in column Location and showing as table in html view.
The result what I want is like this:
id | Name | age | Location | type
---+----------+-----+----------+------
1 | Ane | 22 | SG | 1
3 | Bobby | 23 | PH | 1
4 | Denis | 26 | IND | 1
5 | Jerry | 21 | SG | 1
This is my code:
<?php
$con = mysqli_connect("localhost","root","","testuser");
$query = mysqli_query("SELECT * FROM `usersdata` WHERE `type`='1'");
$result = mysqli_query($con,$query);
echo "<table class='tmaintable' border='0' cellpadding='3' width='99%' align='center' cellspacing='1'>
<tr class='theader'>
<td>ID</td>
<td>Name</td>
<td>Age</td>
<td>Location</td>
<td>Type</td>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr class='todd'>";
echo "<td style='text-align:center;' >" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
But I got errors like this:
Warning: mysqli_query() expects at least 2 parameters, 1 given in www.myweb.com\users_list\type.php on line 94 << this point "$query" line
Warning: mysqli_query(): Empty query in www.myweb.com\users_list\type.php on line 95 << this point "$result" line
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in www.myweb.com\users_list\type.php on line 109 << this point "while ($row=" line
I was trying understand and still i don't get it, anyone can help me please?! thanks.
Change
$query = mysqli_query("SELECT * FROM usersdata WHERE type='1'");
to
$query = "SELECT * FROM usersdata WHERE type='1'";
EDIT
Just for explanation:
mysqli_query takes 2 arguments: connection and query. I assumed that you wanted to just create query string in this line where this error arouse since it is used one line furter as a query string.
Issue is in bolow lines.
$query = mysqli_query("SELECT * FROM usersdata WHERE type='1'");
$result = mysqli_query($con,$query);
You have 2 ways to resolve this.
1. $query = "SELECT * FROM `usersdata` WHERE `type`='1'";
$result = mysqli_query($con,$query);
2. $query = mysqli_query($con,"SELECT * FROM `usersdata` WHERE `type`='1'");
Try this one. It should be working fine
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `usersdata` WHERE `type`='1'";
$result = mysqli_query($conn, $sql);
echo "<table class='tmaintable' border='0' cellpadding='3' width='99%' align='center' cellspacing='1'>
<tr class='theader'>
<td>ID</td>
<td>Name</td>
<td>Age</td>
<td>Location</td>
<td>Type</td>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr class='todd'>";
echo "<td style='text-align:center;' >" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
I'm making a table with translation:
English | Italian | French
help | ayuto | amo
and am creating a functionality to add new language (Russian). I have it all set up but the import doesn't work correctly. It imports the values under already existing fields. So it doesn't add them directly under the newly added language. Looks like this:
English | Italian | French | Russian(added)
help | ayuto | amo |
| | | помощь(imported)
| | | извините(imported)
I need that to start it from the top like this:
English | Italian | French | Russian(added)
help | ayuto | amo | помощь(imported)
| | | извините(imported)
Here is the code:
if (isset($_POST['submit'])){
$lang_name = htmlspecialchars($_POST['lang_name'], ENT_QUOTES);
$str = strtolower($lang_name);
$lang_lawname = str_replace(' ', '_',$str);
$sql = "INSERT INTO translation_lang (languages) VALUES ('".$lang_name."')";
$sql2 = "ALTER TABLE translation ADD $lang_lawname VARCHAR( 255 ) DEFAULT ''";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2)) {
die('Error: ' . mysqli_error($con));
}
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
//echo "<h2>Displaying contents:</h2>";
readfile($_FILES['filename']['tmp_name']);
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
do {
if ($data[0]) {
$import="INSERT into translation($lang_lawname) values('$data[0]')";
mysqli_query($con,$import) or die(mysqli_error());
}
}
while ($data = fgetcsv($handle,1000,",","'"));
}
All i want to do is to store the user entered values to a database.There is a table and it has many text boxes where the user enters his input.Upon clicking submit button the values should be stored in the mysql table.
I'm getting some errors right now.I'm completely new to php,Below I've attached the code and screenshots.
//workingcode.php
<?php
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$result = mysql_query("SELECT * FROM dept_info", $con) or die(mysql_error());
echo "<table border='1'>";
echo '<tr> <th>Sl No</th> <th>USN</th> <th>Name</th><th>code1</th><th>code 2</th> <th>code 3</th> <th>code 4</th> <th>code 5</th>
<th>code 6</th> <th>code 7</th> <th>code 8</th></tr>';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<form action = "insert.php" method = "post">
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>
';
?>
I've linked this to an insert.php file as shown below..
<html>
<head>
<title>Marks Entry Results</title>
</head>
<body>
<h1>Student Marks Entry Results</h1>
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>
</body>
</html>
//The description of the table which i made for this is
/*mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| usn | varchar(50) | NO | PRI | NULL | |
| code1 | int(2) | YES | | NULL | |
| code2 | int(2) | YES | | NULL | |
| code3 | int(2) | YES | | NULL | |
| code4 | int(2) | YES | | NULL | |
| code5 | int(2) | YES | | NULL | |
| code6 | int(2) | YES | | NULL | |
| code7 | int(2) | YES | | NULL | |
| code8 | int(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
*/
The textboxes are not inside any form tag so when you hit submit the value of textboxes doesn't get post back to server...Try following.
echo '<form action = "insert.php" method = "post">';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>';
//insert.php
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>