I have two MySQL tables that I created like this:
CREATE TABLE book(id INT UNSIGNED NOT NULL AUTO_INCREMENT KEY, title VARCHAR(256),
book_id INT, author VARCHAR(128), year INT, httplink VARCHAR(256)) ENGINE MyISAM;
and
CREATE TABLE excerpt(id INT UNSIGNED NOT NULL AUTO_INCREMENT KEY, book_id INT, excerpt_title
VARCHAR(256), year INT, measure VARCHAR(256), page VARCHAR(128)) ENGINE MyISAM;
So there's a table of books...and a table of excerpts from each book...they're linked by book_id. Each unique book has a book_id...and every excerpt from that book has the same book_id.
Now, I have a php form that allows to users to edit a entry after they've searched for it:
<p>Edit Record</p>
<form method="post">
<p>Title:
<input type="text" name="title" value="$n"></p>
<p>Author:
<input type="text" name="author" value="$e"></p>
<p>Year:
<input type="integer" name="year" value="$p"></p>
<p>Mirlyn link:
<input type="text" name="link" value="$l"></p>
<p>Excerpt name:
<input type="text" name="excerpt" value="$ex"></p>
<p>Page numbers
<input type="text" name="page" value="$s"></p>
<input type="hidden" name="id" value="$id">
<p><input type="submit" value="Update"/>
When the user hits the edit button next to the link, the following fields autopopulate with the record the user chose to edit: title, author, year.
If the user does not change the title, but changes (or doesn't change) the other fields, what is the correct MySQL statement to update the record? I believe I'd only have to update the 'excerpt' table.
BUT...if the user edits title...how do I manage that? I'd have to check if the title exists in the 'book' table. If it does, I'd have to change the book_id for the excerpt. If it doesn't, I'd have to add it to the 'book' table with a unique book_id and then change the record appropriately in the excerpt table. Help?
Thanks!
Related
I can't seem to figure this one out, nor how to start my query.
I have a form with different input field ID's
(customer_relationid, customer_brand, customer_goal, customer_floors, ....)
I have a mysql table with 3 columns (auto increment id, Key, Value)
The goal is to post all the info from the form row by row in mysql so that it results in this :
column id
this is just a auto increment, not important in this case
column KEY
relationid
brand
goal
floors
....
column VALUE
15
sony
music
3
.....
This is part of my form (the rest of the fields is the same):
<form id="calculator_form">
<fieldset id="generalInfo">
<legend>General Info</legend>
<h2>General Info</h2>
<label for="calculator_relationID">relationID:</label>
<input type="text" name="calculator_relationID" style="width:250px;" id="calculator_relationID" class="calculator_relationID">
<label for="calculator_brand">Brand:</label>
<input type="text" name="calculator_brand" style="width:250px;" id="calculator_brand" class="calculator_brand">
<label for="calculator_goal">Goal:</label>
<input type="text" name="calculator_goal" style="width:250px;" id="calculator_goal" class="calculator_goal">
<label for="calculator_floors">Floors:</label>
<input type="text" name="calculator_floors" style="width:250px;" id="calculator_floors" class="calculator_floors">
...
</fieldset>
How can I create the query as such so it knows it needs to insert it row by row for each form element?
Tnx in advance!
I have an entry form to post data into a MySQL database (with a submit button) and it works fine. Now I want to have edit, next & previous buttons on it, to get next and previous record and also to edit them if needed.
I have searched on the internet but could not find a solution according to my requirement.
First of all, you should ensure that you have a column for the record ID that's an INTEGER, a PRIMARY KEY, and set to AUTOINCREMENT, let's call this `recordID`.
Let's take an example person table schema:
CREATE TABLE people (
recordID INTEGER PRIMARY KEY AUTOINCREMENT,
firstName VARCHAR(140) NOT NULL,
middleNames VARCHAR(250),
lastName VARCHAR(140) NOT NULL,
dateOfBirth DATE NOT NULL
);
To query the first record, we can do:
SELECT * FROM people WHERE recordID = 1;
Now to edit the record, we can do:
UPDATE people SET firstName="NewName" WHERE recordID = 1;
Next we build an HTML form to display/edit this data in.
<form action="#" method="post">
<input type="text" readonly="readonly" name="recordID" id="recordID" />
<input type="text" name="firstName" id="firstName" />
<input type="text" name="middleNames" id="middleNames" />
<input type="text" name="lastName" id="lastName" />
<input type="date" name="dateOfBirth" id="dateOfBirth" />
<input type="submit" />
</form>
Last of all you create some next and previous buttons to traverse through the records and populate the input fields, and then an edit button that sends the data to the server for it to update the database.
If you're feeling extravagant, you could use SQL's INSERT INTO ... ON DUPLICATE KEY UPDATE. E.g.:
INSERT INTO people (firstName, lastName, middleNames, dateOfBirth) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE firstName="?", lastName="?", middleNames="?", dateOfBirth="?";
In order to do an "in-place edit", you could add a variable to the querystring.
For example, if you want to edit recordID = 3, you could have the URL as: http://yourserver.com/person/?id=3&edit.
On the serverside you can check for edit by using isset($_GET['edit']). If that returns true, than run your edit code and populate the fields/enable the edit functionality.
I have a table for documents and part of that is the docs being assigned to a category. The table for doc_list looks like this:
CREATE TABLE `doc_list` (
`doc_id` int(11) NOT NULL,
`doc_title` varchar(50) NOT NULL,
`doc_content` text NOT NULL,
`doc_created` datetime NOT NULL,
`doc_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
`cat_no` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=122 ;
I am having to manually assign the cat_no (which is the category ID) but want it to be apart of my doc form submission:
<form action="actions/newDocAdd.php" method="post" id="rtf" name="">
<input type="text" name="doc_title" id="doc_title" required="required" placeholder="Document Title"/><br />
<?php
try{
$results = $dbh->query("SELECT * FROM cat_list ORDER BY cat_title ASC ");
}catch(Exception $e) {
echo $e->getMessage();
die();
}
$docs = $results->fetchAll(PDO::FETCH_ASSOC);
foreach($docs as $docs){
echo '
<input type="checkbox" name="cat_no" value="2" id="cat_no">'.$docs["cat_title"].'<br><br>
';}
?>
<textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
<iframe name="editor" id="editor" style="width:100%; height: 600px;"></iframe>
<br><br>
<input onclick="formsubmit()" type="submit" value="Create Document" name="submit"/>
</form>
Now it shows me the categories and displays them as checkboxes but because some documents can be apart of more than one category i want to tick the boxes and submit them to the database.
Here is the category table:
CREATE TABLE `cat_list` (
`cat_id` int(11) NOT NULL,
`cat_title` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=5 ;
Assuming documents can have multiple categories:
First you need to group the checkboxes into an array. You can do this by using the same name with brackets.
<input type="checkbox" name="cat_no[]" value="1" />
<input type="checkbox" name="cat_no[]" value="2" />
<input type="checkbox" name="cat_no[]" value="3" />
Now when you submit the form, $_POST['cat_no'] will be an array containing the checked values.
For proper database normalization, you need a new table called something like 'doc_category'. In this table, you would have a composite key of 'doc_id' and 'cat_id':
doc_id | cat_id
1 | 1
1 | 2
1 | 3
This signifies that doc_id 1 belongs to the categories 1, 2, and 3.
The relationship from documents to categories is called a many-to-many relationship which in databases, requires a third table (intermediate).
I have the following form in my main page:
<left><h2><font color="ghostwhite">Enter Part</font></h2></left>
<form action="addpart.php" method="post">
<font color="ghostwhite">Date:   &   </font> <input type="text" name="date" /><br>
<font color="ghostwhite">Part Number:     </font> <input type="text" name="partnum" /><br>
<font color="ghostwhite">Location:     </font> <input type="text" name="location" /><br>
<font color="ghostwhite">Quantity:     </font> <input type="text" name="quantity" /><br>
<input type="submit" />
</form>
I have the following php that I'm wanting to do the following:
Insert the $date entered by the user into the date column in the project database.
Insert the $partnumber entered by the user into the part_name column in the project database.
Insert the $location entered by the user into the location column in the project database.
Insert the $quantity entered by the user into the quantity column in the project database.
Here is the problem I'm having. I can't figure out how to search the part_name column for what the user entered and if it's not there enter it in with the quantity the user specified, but increment by the user specified amount if it is found.
I'm thinking it would be something like:
$dup = mysql_query("SELECT * FROM parts(part_name) WHERE VALUE($partnumber)
if ($dup)
{
mysql_query("UPDATE parts SET part_name = part_name + $quantity");
}
else
{
mysql_query("INSERT INTO parts (time, part_name, location, quantity) VALUES($date, $partnumber, $location, $quantity)");
}
Any help would be appreciated, thanks.
Have a look at:
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Should do what your asking
No comments on the html.
I am trying to attach a numerical "post ID" value to comments so that they can be retrieved from the database and displayed in the proper place. How do I establish this numerical value within my html form as something that gets sent to the script that inserts it into the database? I assume I need to use GET or POST but I don't understand how to use those to send anything except text entered by the user.
This is the form I am using to send the "name" and "comment" inputs:
<div class="comments">
<form action="foxpost.php" method="post">
<label for="name">Name</label><br>
<input id="name" name="name" type="text" /><br>
<label for="message">Comment</label><br>
<textarea class="message" id="message" name="message"></textarea><br><br>
<input type="Submit" value="Post Comment" />
</form>
</div>
Since you tagged this question with PHP, I'm guessing thats the language your using for your back-end. Another assumption I'm making is that your actually formatting your request querystring with the postID, something like "http://example.com/posts.php?postID=1212", notice the postID in the querystring, you just pass that on, like this:
<div class="comments">
<form action="foxpost.php?postID=<%= $_GET['postID'] %>" method="post">
<label for="name">Name</label><br>
<input id="name" name="name" type="text" /><br>
<label for="message">Comment</label><br>
<textarea class="message" id="message" name="message"></textarea><br><br>
<input type="Submit" value="Post Comment" />
</form>
</div>
Using
<%= $_GET['postID'] %>
will simply echo the postID from the querystring straight into the HTML, or you could assign it to a variable.
If you are using mysql you don't need to create it, mysql can auto-create it when you insert a new post.
For example we could create a table
CREATE TABLE `student` (
`student_id` INT( 3 ) NOT NULL AUTO_INCREMENT,
`name` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 50 ) NOT NULL ,
UNIQUE ( `student_id` )
);
And then use the following query
INSERT INTO `student` ( `name` , `email` ) VALUES ( 'john', 'email' );
As you can see the id is not specified in the query, but the field has the AUTO_INCREMENT attribute. When you insert a student without an id it will get the highest id and add one. So if you have the empty table and run the above insert query, you will get 3 rows with id 1,2 and 3.
More in the mysql manual http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
p.s. if you are using a different database please specify wich one.
--- EDIT 1 ---
might have misread the question very badly (it's past midnight but that's not an excuse)
You have a few options if you want to pass
1. a hiddent field, that was mention here
2. a $_GET['postid'] from something like comment.php?postid=13 if you add comments from another page
3. both
BUT don't forget, before adding the comment that the post exists.
... might i suggest using the akismet library to cut down on spam ?
http://www.achingbrain.net/stuff/php/akismet
You can get a key for free when you register at wordpress.com
Put the id in a hidden field in your form:
<input type="hidden" name="post_id" value="id_goes_here" />
I would suggest to not have the ID as part of the form and just use your databases AUTO_INCREMENT feature.