I have looked thoroughly, but I have not found the solution (or perhaps I didn't realize I had since I'm a novice)... Here is my problem:
My table has these six fields: username,want1,want2,want3,want4,want5. My form has username, and a multiple SELECT box where five wishes are selected. I want to insert that username and those selections together as a record, in their corresponding "cell" in the table. Here is my code (for the submit/$_POST action) so far:
*Note: I have named the multiple SELECT box "wants[]" so that it knows it's an array.
<?php
//variables
$username = $_POST['username'];
$want = $_POST['wants'];
//connect
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//insert
mysql_query("INSERT INTO The_Table (username,want1,want2,want3,want4,want5) VALUES ('$username','$want[0]','$want[1]','$want[2]','$want[3]','$want[4]')");
?>
When I test, no bugs/errors show, but the table isn't being filled with a record. I am very new to coding, I apologize. Hopefully there is a simple solution. Thank you so much to any help or advice!!
Try this code i hope its work for you...
form.php
<form action="insert.php" method="post" name="form">
user_name : <input name="u_name" type="text" /><br />
wish 1 : <input name="wish_1" type="checkbox" value="1" /><br />
wish 2 : <input name="wish_2" type="checkbox" value="2"/><br />
wish 3 : <input name="wish_3" type="checkbox" value="3"/><br />
wish 4 : <input name="wish_4" type="checkbox" value="4"/><br />
wish 5 : <input name="wish_5" type="checkbox" value="5"/><br />
<input type="submit" />
</form>
insert.php
<?php
//variables
$username = isset($_POST['u_name'])?$_POST['u_name']:'';
$wish_1 = isset($_POST['wish_1'])?$_POST['wish_1']:'';
$wish_2 = isset($_POST['wish_2'])?$_POST['wish_2']:'';
$wish_3 = isset($_POST['wish_3'])?$_POST['wish_3']:'';
$wish_4 = isset($_POST['wish_4'])?$_POST['wish_4']:'';
$wish_5 = isset($_POST['wish_5'])?$_POST['wish_5']:'';
//connect
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//insert
mysql_query("INSERT INTO The_Table (username,want1,want2,want3,want4,want5)
VALUES ('".$username."','".$wish_1."','".$wish_2."'
,'".$wish_3."','".$wish_4."','".$wish_5."')");
?>
In your description, you say you named the select "wishes" and the fields "wish1", "wish2" etc., but then in your code you call it ['wants'] and "want1, want2". If that's really how your code is, it would cause a problem...
Related
This has to be somewhere online but I am having no luck after hours of trying to do this.
So I've HTML form on one page and a PHP page that creates a database fine..
<form action="createdb.php" method="post">
<label for="dbname"><b>Name of DB</b></label>
<input type="text" name="dbname" id="dbname"/>
<input type="submit" value="Create DB">
<?php
$conn = mysqli_connect("localhost", "root", "") or die(mysqli_error());
$dbname = $_POST['dbname'];
if (mysqli_query($conn,"CREATE DATABASE $dbname")) {
echo "Database created";
} else {
echo "Database was not created";
}
mysqli_close($conn);
?>
Then I have underneath the PHP code these forms.. The form for creating the tables work fine within the DB that has just been created.. But its the form for copying tables from a DB already created into the newly created DB.
<form action="createtable.php" method="post">
<label for="tablename"><b>Create Table within new DB</b></label>
<input type="text" name="tablename" id="tablename"/>
<input type="hidden" name="holdname" value="<?php echo $dbname ?>">
<input type="submit" value="Create Table">
</form>
<p>OR</p>
<form action="copytables.php" method="post">
<label for="tablename"><b>Copy RSS Tables</b></label>
<input type="text" name="tablename" id="tablename" readonly/>
<input type="hidden" name="holdname" value="<?php echo $dbname ?>">
<input type="submit" value="Copy Tables">
</form>
I wanted to copy the tables, structure and data called 'lookup_age' and 'score' into the new DB from a database called 'rss_db'. I've rewrote the PHP page needed in many different ways and ATM it has been left like this, as of something I seen on W3schools, which confused me even more. I know it can be easily done via PHPMYADMIN but need it through a query now and HTML form if possible. Heres what I have as followed but wondering what should the query line actually be if possible..
<?php
$conn = mysqli_connect("localhost", "root", "") or die(mysqli_error());
$dbname =$_POST['holdname'];
mysqli_select_db($conn,"$dbname");
mysqli_select_db($conn,"rss_db");
$sql = "
INSERT lookup_age
INTO $dbname
FROM rss_db";
mysqli_close($conn);
?>
I don't know if these will help but you could try:
create table `table2` like `table1`;
insert `table2` select * from `table`;
or, as a single line perhaps
create table `table2` as select * from `table1`;
try this query, to copy the tables:
$sql = " create table '$dbname' as select * FROM 'rss_db'";
FINALLY FOUND IT!! As simple as...
$query = "INSERT INTO $dbname.lookup_age
SELECT * FROM rss_db.lookup_age";
So I'm creating a small program with 2 forms, one to add data to a database, and one to delete from it. I've managed to create the first input form, but I'm slightly confused as to how I would get the second form to work. In the database "tasks" I have a table called "ID" which has the columns "ID", "Name" and "Hours"
Here's what the two HTML forms look like
<h2>Add Tasks</h2>
<form action="test.php" method="get">
Name of Task: <input type="text" name="name"><br />
Hours: <input type="number" name="hours"><br />
<input type="submit" value="Add" name="submit">
</form>
<h2>Delete Tasks</h2>
<form action="delete.php" method="get">
ID: <input type="number" name="ID"><br />
<input type="submit" value="Delete">
</form>
And the PHP for the first form "Add tasks" which inserts data
$servername = "localhost";
$username = "root";
$password = "root";
$conn = new mysqli($servername, $username, $password, "Tasks");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
};
if (isset($_GET['submit'])) {
mysqli_select_db ($conn,"Tasks");
$name = $_GET['name'];
$hours = $_GET['hours'];
$sql = "INSERT INTO ID (Name, Hours) VALUES ('".$name."','". $hours."')";
$results = mysqli_query($conn,$sql);
$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;
And the PHP for the second form which deletes tasks. This is the part that is not working
if (isset($_GET['submit'])) {
mysqli_select_db ($conn, "Tasks");
$id = $_GET['id'];
$sql = "DELETE FROM ID (ID) VALUES ('".$id."')";
$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;
How should I format the PHP for the second button. I've basically reused the code for the first form. Do I need to differentiate it somehow from the first button? Currently the page is showing up completely blank. I'm a complete novice so any help would be appreciated.
Your SQL Statement
"DELETE FROM ID (ID) VALUES ('".$id."')"
is wrong.
It should be
DELETE FROM table_name
WHERE some_column=some_value;
. So, change your statement to
DELETE FROM ID WHERE ID='$id'
Suggestions
You should use POST method for action which will result in data edit.
You should check the input, make sure it did not contain SQL statement. A good way is to use $stuff = mysql_real_escape_string($_GET["stuff"]).
I see you have name 'ID' in the form but your are trying to get 'id'. That could be the problem
The sql statement for deletion should look something like the snippet below.
$sql = "DELETE FROM ID WHERE `id`=".$id.";";
$results = mysqli_query($conn,$sql);
In addition to above answers you should give different name to the both form input tags as
<h2>Add Tasks</h2>
<form action="test.php" method="get">
Name of Task: <input type="text" name="name"><br />
Hours: <input type="number" name="hours"><br />
<input type="submit" value="Add" name="submit">
</form>
<h2>Delete Tasks</h2>
<form action="delete.php" method="get">
ID: <input type="number" name="ID"><br />
<input type="submit" value="Delete" name="delete">
</form>
So for adding into database , you can use
if (isset($_GET['submit'])){
// your code here
}
And for deleting from database , you can use
if (isset($_GET['delete'])){
mysqli_select_db ($conn, "Tasks");
$id = $_GET['id'];
$sql = "DELETE FROM ID (ID) WHERE ID='".mysql_real_escape_string($id)."' ;
$query = "SELECT `Name` FROM `ID`";
$result = mysqli_query($conn, $query);
$x=0;
}
This will solve all the problems.
If you are using same name for the type="submit" in both forms than you can use POST method on one form and GET method on the other.
And yes mysql_real_escape_string is used to prevent SQL INJECTION.
so im trying to store values from a checkbox into my database
It works if I use a normal Textbox but as soon as I attempt it with a checkbox it doesnt work any idea? I want to have two for example checkbox1 and checkbox2 there values should be stored in my database colums for example Colum1 colum2.
Thanks in advance for anyhelp
<form name="checkbox.php" id="names" action="<?php echo JURI::current(); ?>" method="post">
<p><input type="checkbox" name="game" value="ExampleGame" />b</p>
<p><input type="checkbox" name="Age" value="ExampleAge" />b</p>
<p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
<?
if( (isset($_POST['game'])) || (isset($_POST['Age'])) ) {
//first name or last name set, continue-->
$game = $_POST['game'];
$Age= $_POST['Age'];
$db =& JFactory::getDBO();
$query = "INSERT INTO `gameTable` (`game`, `Age`)
VALUES ($game, $age);";
$db->setQuery( $query );
$db->query();
} else {
echo '<h4>One Field Is Required!</h4>';
}
?>
Try this
$query = "INSERT INTO `gameTable` (`game`, `Age`) VALUES ('".$game."','".$age."', )";
Check the values that come back from your form:
$game = $_POST['game'];
$Age= $_POST['Age'];
You should find that if the checkbox isn't selected, no value (in fact, no field) is returned.
That may be your problem.
Use some alerts for troubleshooting:
echo $_POST['game'];
echo $_POST['Age'];
echo $_POST['query'];
Even when you don't know what you are doing/doing wrong, try to troubleshoot the problem.
Alerts help alot in PHP to check if you get the values on your variables that you expect.
If you get your query string, test this directly on your database.
I resolve with this easy way:
<input type="checkbox" name="field_name" value="N" style="display: none;" checked />
On mysql database i have create a trigger
if new.field_name= '' then set new.field_name= 'S';
I want to have a form that allows the user to choose what data to display from a table through checking the checkboxes. If the user wants only 2 columns to be shown, should only 2 columns be shown. I have my codes, but after I submit, it displays nothing.Here's my code:
<form name="form1" method="post" action="view_emp.php">
<p>Select Option
<input type="checkbox" name="number[]" value="name" />Name
<input type="checkbox" name="number[]" value="hired" />Date Hired
<input type="checkbox" name="number[]" value="basic" />Basic Pay
<input type="checkbox" name="number[]" value="incentives">Incentives
</p>
<input type="submit" name="Submit" value="Submit">
</form>
here's my php:
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('eis', $db) or die (mysql_error());
$employee = array();
foreach ($_POST['number'] as $employee) {
$number = mysql_real_escape_string($number);
$employee[] = "'{$number}'";
}
$sql = "select * from employees where type in (" .implode(", ", $number). ")";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
print $row['name'];
}
?>
i am a beginner in php and i need help from gurus and experts. thank you...
PHP's implode() and explode() functions might come in handy. You can easily turn your POST or GET attribute, 'number', into a comma-separated list by using implode($_POST['number']). This would be easy to store in one MySQL field, maybe a column in your user table.
If you want users to edit the form later, render the checkboxes with a loop and add a "checked" attribute to each checkbox element whose name exists in 'exploded' list (array) retrieved from your database.
This is basically serialization/deserialization. There are other ways to do it including serialize(), unserialize() or json_encode(), json_decode(). But since your data seems to be easily modeled as a basic list, you can keep it even simpler.
I'm creating a forum in PHP and using MySql as a database, and was wondering how I could check if a MySql field topic_locked was equal to 1. If it isn't, the reply code would be displayed. How can I check this, and if you can help me find how to check this, how could I set it to 1 through the forum?
I dont know your code. But I am sharing simple program to check a mysql field's value.
<?php
// Database select and connect to host
$sql= mysql_query("SELECT topic_locked FROM table WHERE Id='your_id'");
$res= mysql_fetch_array($sql);
$value= $res['topic_locked'];
if($value=='1')
{
// reply code
}
?>
Update asked,
<?php
if(isset($_POST['update']))
{
$id= $_POST['id'];
//Database select and connect to host
mysql_query("UPDATE table SET topic_locked='1' WHERE Id='$id'");
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="your_id" />
<input type="submit" name="update" />
</form>