Set value of variable with hyperlink php [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
I want to set the value of a column in my database by the link a user clicks.
<body>
Choose the position you would like your duo to primarily play:
<a href="/ADC%20Seek/ADC-Support.html">
Support
</a>
<a href="/ADC-Seek/ADC-Mid.html">
Mid Lane
</a>
<a href="/ADC-Seek/ADC-Top.html">
Top Lane
</a>
<a href="/ADC-Seek/ADC-Jungle.html">
Jungle
</a>
</body>
<?php
$seek= value set from link clicked
$query= "INSERT INTO mytable (seek) VALUES ('$seek')";
?>

There are many ways you can do this, you can pass a variable into the URL using a GET but that would require you to change your page structure.
Simply add the insert on each page:
ADC-Jungle.php
$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "INSERT INTO db (column_name) VALUES ('Jungle');
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
For your purpose this is the simplest solution, but not necessarily the way I would do it.
EDIT
As Marc said, these need to be PHP files.

Related

Dynamic Drop Down List using loop [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 5 years ago.
Improve this question
I have database named as 'ecc' with table named as 'client'
table client has many fields, consider one of them named as ProjectManager
I want to display data of ProjectManager in select drop down list.
More data can be added to ProjectManager so loop is necessary.
please can some one help by writing the code for the same...
thanks for help in advance..
Below is a sample code for mysqli:
I did not given that a try but should be working. You need to replace values for username, password for db and what value you need in your select options
<?php
// Connect db
$mysqli = new mysqli('localhost','username','password','ecc');
// check if error
if ($mysqli->connect_error) {
die('Can not connect to DB error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
// MySqli Select Query
$results = $mysqli->query("SELECT ProjectManager FROM client");
echo '<select name="project_manager">';
echo '<option value="">-select project manager-</option>';
while($row = $results->fetch_assoc()) {
echo '<option value="'.$row['ProjectManager'].'">'.$row['ProjectManager'].'</option>';
}
echo '</select>';
// Frees memory
$results->free();
// close connection
$mysqli->close();
?>

Php execute lines [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
I want to execute these lines When I click on the button:
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
It will pick another word from the database using that query.
The button in the HTML page:
echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
How can I do this?
To actually run the query you can use MySQLi. Here is a good beginners guide.
//Connect to database.
$db = new mysqli('localhost', 'user', 'pass', 'database');
//The code you provided.
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
//Run the query.
$result = $db->query($sql);
//Get the first (and presumably only) row
$row = $result->fetch_assoc();
//Output the word.
echo $row['word'];
//Free up some memory.
$result->free();
To get the link to work, you need to have the name of the page. You can either hardcode it, or use $_SERVER['PHP_SELF']:
echo 'Play again!'
(I don't know what you use the $n for so I just left it in there.)

How to edit the value updated in a database from the form using php and mysql [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 7 years ago.
Improve this question
I want to edit the data which is already in the databse.
Suppose I have updated my database, but after an year the user who has updated that database want to edit something like his address which is changed now through the form, then how can we do that. I am posting
<?php
// Create Local variable
$taken = "false";
$database = "railway";
$password = "";
$username = "root";
// Main if statement
//if($userreg && $passreg){
// Connect to database
$con = mysqli_connect('localhost', $username, $password,$database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
---------
WHAT TO WRITE IN BETWEEN
----------------
mysqli_close($con);
Suppose my table name is Railway and the attributes are time, name, station_to and station_from. I want to change the name.
Please write both the form as well as php and mysql query.
It would be something like this:
$sql = "UPDATE Railway SET name='Joe' WHERE id=5";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

How do i make a list of items that takes data from a MySQL database [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 have an HTML/CSS "contentbox"(It's basically a box that includes an image, title, description, name, etc of a product or item) and i have a database table with all this information for a lot of items/products.
I want to take all this data from product1(title, image, description) from the database and put it in my contentbox.
And i want to do this with all my products from my table and display all this contentboxes below each other. Sort of like the craigslist for sale ads that are in a list.
How do i do this? do i need javascript or where do i start? I don't know much about programming and i'm learning as i go.
I realize i should put some code in my questions so here goes another question:
I have a div container that looks like this:
<div class="contentBox">
<div id="column1">
<img src="images/gallery/girlthinking.jpg" alt="" id="imagen">
</div>
<div id="column2">
<p class="tituloanuncio"><b>Titulo del anuncio</b></p>
<p class="descripcionanuncio">Descripcion kfewoijfew</p>
</div>
<div id="column3">
<p class="precioanuncio"><b>$1000</b></p>
<p class="contactoanuncio"><b>Contacto<br></b>DueƱo: Alejandro<br>Telefono: 8331578460<br>jorgegilcavazos#gmail.com<br>Facebook</p>
</div>
</div>
It works in html, i want to replace the default values a put in there with data from my table, for example where it says.
<div id="column2">
<p class="tituloanuncio"><b>Titulo del anuncio</b></p>
<p class="descripcionanuncio">Descripcion kfewoijfew/p>
</div>
I would like to replace "titulo del anuncio" with the column 'anuncio_titulo' and replace "descripcion kfewoijfew" with the column 'anuncio_descripcion'.
I have this php code that displays all data from the 2 previus columns i mencioned
$result = mysqli_query($con,"SELECT * FROM anuncio");
while($row = mysqli_fetch_array($result)) {
echo $row['anuncio_titulo'];
echo $row['anuncio_descripcion'];
}
How do i echo my initial html div container replacing the default text i put with the new columns?
I hope this makes sense.
You need to first fetch the data from database (read about MySQL, mysqli PHP function). Then you need to pass the data to the site that user sees using PHP (read about PHP). Finally you need to display the data with HTML (read about HTML with PHP).
Nice explanation of mysqli functions is here. To fetch data:
Connect
Create query
Execute query
Get data from query
First connect:
$db = new mysqli('localhost', 'user', 'pass', 'demo');
// check if connection succeeded
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
Then create and execute query:
$sql = "SELECT * FROM `users` WHERE `live` = 1"
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
and get data from query
$usernames = array();
while($row = $result->fetch_assoc()){
$usernames[] = $row['username'] . '<br />';
}
Once you have an array, pass it to your view and print, for example as a part of a table:
<table>
<?php foreach($username in $usernames): ?>
<tr><td><?php $username ?> </td></tr>
<?php endforeach;?>
</table>
If you do not need to change data without reloading the page, you don't need javascript or ajax. I can see several ways you can do what you intend to, but will just explain the easiest one.
First is to assign the text to the variable and print it. If it needs to be changed with data from databse then just assign the value to that variable.
<?php
$title = "Titulo del anucio";
$desc = "Descripcion kfewoijfew";
// it has default values, let's say you want to fill them with data from db now
if(/* some condition here, like data from GET : $_GET['change_data'] == true or something */) {
$db = new mysqli('localhost', 'user', 'pass', 'demo');
// (...) and so on like I wrote above, query the data and assign to value
$res = $result->fetch_assoc() // if you know there is one result you can shorten it
$desc = $res['anuncio_descripcion'];
$title = $res['anuncio_titulo'];
}
?>
<div id="column2">
<p class="tituloanuncio"><b><?php echo $title;?></b></p>
<p class="descripcionanuncio"><?php echo $desc?>/p>
</div>

Trouble executing transaction in PHP [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 8 years ago.
Improve this question
I'm having trouble getting a mysql transaction to work through PHP. I'm fairly new to PHP and very new to mysql.
If I take the var_dump of $query and try to run it through phpmyadmin it works fine.
$description =
mysql_real_escape_string($_REQUEST['description']);
$query = 'BEGIN;
INSERT INTO indiespark.tasks
(description, owner_user_id)
VALUES ("' . $description . '", '
. $user->user_id . ');
SET #task_id = LAST_INSERT_ID();
INSERT INTO indiespark.projecttasks
(task_id, project_id)
VALUES (#task_id, ' . $project->project_id . ');
COMMIT;';
$result = mysql_query($query);
var_dump($query);
var_dump($result);
if ($result) {
return viewproject();
} else {
throw new Exception('database error');
}
mysql_query doesn't support sending multiple queries in one call. Use separate calls.

Categories