I have a table that displays fields sent from a form. There are buttons that can edit or delete selected row by selecting id. I want to add a button that would insert selected row to another table. I cannot get it to work.
Here's the code for the table:
<?php
/*
VIEW.PHP
Displays all data from 'players' table
*/
// connect to the database
include('config2.php');
// get results from database
$result = mysql_query("SELECT * FROM articles")
or die(mysql_error());
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Author</th> <th>Email</th> <th>Title</th> <th>Poem</th> <th>id</th>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['Name'] . '</td>';
echo '<td>' . $row['Email'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['content'] . '</td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo '<td><a href="publish.php?id=' . $row['id'] . '">Publish</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
Here's the code for delete function:
// connect to the database
include('config2.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM stories WHERE id=$id")
or die(mysql_error());
// redirect back to the view page
header("Location: secret.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: secret.php");
}
And here's how I think the function to insert the row to other table should look like but its not working
// connect to the database
include('config2.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id values
$id = $_GET['id'];
$name = $_GET['name'];
$email = $_GET['email'];
$title = $_GET['title'];
$content = $_GET['content'];
//upload
$result = mysql_query("INSERT into publish (name, email, title, content)
VALUES WHERE name=$name, email=$email, title=$title, content=$content")
or die(mysql_error());
// redirect back to the view page
header("Location: secret.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: secret.php");
}
I'm new at this so not sure what the correct syntax would look like in this case
Using select query
$id = $_GET['id'];
$result = mysql_query("select *stories WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$query= mysql_query("INSERT INTO publish (name, email, title, content)
VALUES ('$row['Name']','$row['Email']',$row['title'],$row['content'])");
I am not sure about any PHP
related stiff but have you looked at your INSERT
statement. It's completely wrong. You can't use a WHERE
condition in INSERT
statement as shown below
INSERT into publish (name, email, title, content)
VALUES WHERE name=$name, ....
^------ Here
Did you rather meant to use INSERT INTO .. SELECT FROM
construct like
INSERT into publish (name, email, title, content)
SELECT name, email, title, content
FROM Article
WHERE name=$name, email=$email, title=$title, content=$content"
(OR) just an INSERT
statement
INSERT into publish (name, email, title, content)
VALUES($name, $email, $title, $content)
Use sub select if u want but i dont know about performance in large table
Insert into tablea (name,xx,xxx) value ('select name from table b where id=x' ,'select xx from table b where id=x ', 'select xxx from table b where id=x') not tested but it shoild work