Making posts,getting data from mysql table [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how i can make example: post.php and when somebody goes to post.php to show all posts and when somebody click on one post to show in URL post.php?id=1(id=1 by id in database? and when it types post.php?id=2 to go to id 2 in database and show all datas from row of table by id 2)

Use the $_GET method to pass the url. At the top of the php file you can access the information posted in the url using the global $_GET['id']. You can check if it is set, and depending on whether or not it is, show information regarding that id from the database.
It might look something like this:
if (isset($_GET["id"])) {
$id = $_GET["id"];
$query = "SELECT * table WHERE id = {$id} LIMIT 1;";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)){
echo $row["id"];
echo $row["name"];
echo $row["someOtherAttribute"];
}
}
Make sure you have your connection and your database set up and whatnot, but thats how you would accomplish this.

Related

How to get only a text corresponding to the id from MySQL and store it as a variable in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm a begginer to PHP and I want to know how can I fetch some text from the corresponding ID and store it as a variable in PHP.
The table is like
ID----NAME----ACCOUNT----PASSWORD
1----name1----accont1----password2
2----name2----accont2----password2
3----name3----accont3----password3
Now if I want to get the account2 as text and save it in an variable (say acc2) then what should I do. Assuming that I have connection information in connect.php.
Edit: I want to select the account2 using the ID like from ID 2 select account.
Thanks In Advance!!!
Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:
$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']

Updating data in a mysql table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to code a website where users have profile pages. I want them to be able to edit their profile information that they have already entered. But there is an about me section where users are expected to include a large amount of detail about themselves.
I have used the SQL UPDATE statement which works fine to override the data which is already there but I'm wondering if there is a way to extract the data that is there and allow the user to edit this and then post the updated version back to the table?
Any help on an approach to this would be great.
You simply have to make a SELECT to display user infos on their user page and incorporate the values into a form, and then you will handle the form to update the datas (either in the same php file or another one)
I suggest you to visit this tutorial : Create user editing area
The below code fetch the user info from the database and display it in a form.
<?php $sql = "SELECT * FROM user_table where username = '$username'";
$query = mysqli_query($database_connection, $sql);
$fetch = mysqli_fetch_assoc($query);
echo "<form action='update_user_info.php' method='post'>
<input type='text' name='full_name' value='$fetch['full_name']' >
<input type='text' name='address' value='$fetch['address']' >
<textarea title='about' name='about'>$fetch['about_user']</textarea>
<input type='submit' name='btn_update' value='Save'>
</form>?>
Here is what the update_user_info will look like.
<?php $sql = "UPDATE user_table SET full_name = '$_POST['full_name']' WHERE username = '$username'";
$query = mysqli_query($database_connection, $sql);
//I hope you can find your way from here ?>

MySQL search is too "loose" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Alright so I have made a site, just for a project I was doing. On this site I have this database that has a couple of articles on it, I use this code for make queries:
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM articles WHERE title LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo $row['title'];
echo $row['summary'];
echo $row['content'];
echo $row['publicationDate'];
}
}
?>
The problem is that the search is a bit too "loose." The database which the site connects to has many articles. What I don't like is that say I searched the letter 'A' in the search box, it comes up with any instance the letter 'A' appears in any article title. So instead of coming with the article for the letter 'A', Australia, Austria, Afghanistan and even Ecuador come up. Its kind of frustrating. Can you help me?
You can try to force it to be a start of a string or start of a word:
$sql = "SELECT * FROM articles WHERE title LIKE '% ".$term."%' OR title LIKE '".$term."%'";

Mysqli get array from database and format to string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to get array of fields from database:
$query=("SELECT firstname FROM users");
$res = $connect->query($query);
Target is to display first name of every users:
echo $firstname
Output:
Firstname1,Firstname2,Firstname3
I know should be easy, if I just knew more.
For getting all records or a set of records you have to fetch each records inside a while loop.
$query=mysqli_query($connect,"SELECT firstname FROM users"); // execute the query
$string="";
while ($row = mysqli_fetch_array($query)) // fetch each records
{
$string.=$row['firstname']).','; // append the firstname to a variable
}
echo $string; // output the constructed variable with all firstnames

How to display row from page ID [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am very new and trying to get from the url ie
device.php?id=2
and use the id number multiple times.
can anyone tell me how to display this?
Thanks in advance
1) Get the ID.
$id = $_GET["id"];
2) Search for $id in your database, using MySQL functions like mysqli_fetch_array() etc.
3) Echo the values you fetched, using simple HTML and PHP.
$id = $_GET['id'];
$sql= mysql_query("select * from tableName where id = $id ");
while($row = mysql_fetch_array($sql))
{
echo $row['columnName'];
}
The variable should be in $_GET["id"].
<?php
echo $_GET['id'];
?>

Categories