Adding a radio button against each record - mysql

The following code uses the DB extention provided by PEAR.
I want a radio button against each record so that the user can select only one record from the list and submit. There can be hundreds of records from table.
// Proceed with getting some data...
$res =& $db->query('SELECT * FROM someTable limit 10');
while ($res->fetchInto($row)) {
echo "<tr><td>";
echo $row[0] . "\n</td><td>";
echo $row[1] . "\n </td><td>";
echo $row[2] . "\n</td><td>";
echo $row[5] . "\n</td><td>";
echo $row[6] . "\n</td>";
echo "</tr>";
}

ok so put an <input type="radio" name="same-name-for-each" value="$this_record" /> between each table cell.
each radio button gets the same name
the value of each radio button needs to be different - the id or value of whichever option the selected radio represents.

Related

Getting PHP and SQL scripting to work properly

So, from what I have been learning for these past few weeks I believe I have sufficient knowledge on how to perform PHP, and SQL related queries to create a good and dynamic website that could support something like a forum. I've not been able to do that yet, and am having quite a bit of trouble with it as well. So far, I've made a PHP file, that was simply to see if I could use PHP well. It did not work out, and I've been getting plenty of errors, and I've been unable to fix them, whatsoever. And so, I'd like to come here to ask, if anyone out there could possibly analyze my code that I've written, and see what is wrong with it, if possible. Along with that, I'd like to know what would be the "Proper" way of
A. Connecting to SQL
B. Selecting Data
C. Displaying/Utilizing Data
And thank you, for reading and/or possibly replying to this.
Here, is the code I've written but have been unable to work.
<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form>
Input First name:<br>
<input type="text" name="FN">
<br>
Input Last name:<br>
<input type="text" name="LN">
<br>
Input Email:<br>
<input type="text" name="Email">
<br>
<input type="submit" method="post">
<?php
if (isset($_POST['FN'], $_POST['LN'], $_POST['Email']))
$sql = 'INSERT INTO `info` ("USERID", "FN", "LN", "Email") VALUES (\'$_POST[FN]\', '$_POST["LN"]', '$_POST["Email"]')';
?>
</form>
<?php
$sql = "SELECT FN, LN, Email
FROM
info"
$result = "mysql_query($sql)"
while($row_list = mysql_fetch_assoc( $result )) {
ECHO <div>The Names are:</div><br>
ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
Your PHP code is wrong in so many ways even in your query. What I did is clean your codes.
<?php
include 'header.php';
include 'connect.php';
?>
<body>
<form action="" method="POST">
Input First name:<br>
<input type="text" name="FN">
<br>
Input Last name:<br>
<input type="text" name="LN">
<br>
Input Email:<br>
<input type="text" name="Email">
<br>
<input type="submit" name="submit-btn" value="submit">
</form>
<?php
if (isset($_POST['submit-btn'])){
$sql = 'INSERT INTO info ( "FN", "LN", "Email") VALUES ('$_POST[FN]', '$_POST["LN"]', '$_POST["Email"]')';
if (mysql_query($sql)) {
echo "New record created successfully";
}
}
$sql = "SELECT FN, LN, Email FROM info";
$result = mysql_query($sql)
while($row_list = mysql_fetch_assoc( $result )) {
ECHO '<div>The Names are:</div><br>';
ECHO $FN . "," . $LN . "," . $Email;
}
?>
</body>
</html>
try to indent your code to make it more readable for yourself.
as already answered by user3814670, your insert query was wrong, with 4 elements (id,fn,ln,email) and only 3 data (fn,ln,email)
your query was't being executed also cleaned by user3814670 by adding the lines
if (mysql_query($sql)) {
echo "New record created successfully";
}
try to print your query to the screen and executing it in you database to see if your query fails or print the error to screen
mysql_error()
add this on top of your file after
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Here's how you display data from the database using while loop
while($row=mysql_fetch_array($result)) {
echo $row['FN'] . " " . $row['LN'] . " " . $row['Email'];
}

How can I allow user to upload own PDF and have it displayed?

I have a website for school where every teacher is going to have a page. Each teacher's page will have a spot for them to upload a PDF. I want this to then show in a viewer on the page so students see the Viewer when they access it.
How would I code into the website allowing the user to upload a PDF and not have it replaced until somebody else uploads a PDF?
so far I have the code to upload a document.
<form method="POST" enctype="multipart/form-data" action="fup.cgi">
File to upload: <input type="file" name="upfile"><br/>
Notes about the file: <input type="text" name="note"><br/>
<br/>
<input type="submit" value="Press"> to upload the file!
</form>
How can I get it to go into a viewer below? and that it saves until replaced.
1 - First thing you are not able to upload file to server but your form action is claiming to use CGI,
2 - Second i cant really get what you want but the following code can upload files to server its in PHP and otherthing are you using SQL or what Database are you using it seems you also need database
<?php
set_time_limit(0);
if(!is_dir("uploads")){
$uploadDir = mkdir("uploads");
}
$allowedExts = array("pdf", "docx", "doc");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 200000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("/uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Do you want to display pdf thumb, icon, or read the pdf

Codeigniter form elements as table

I'm trying to build a form in Codeigniter so that its elements are part of a table and are therefore aligned nicely. Here's the view page:
<div id="login">
<h3>Log in</h3>
<?php
$attributes = array('id'=>'form_login');
echo validation_errors();
echo form_open('login/main', $attributes);
//probably a bad idea to load libraries in views, but what the heck?!
$this->load->library('table');
$this->table->add_row('Username', form_input('username'));
echo $this->table->generate();
//echo 'Username: ';
//echo form_input('username') . '</br>';
echo 'Password: ';
echo form_password('password') . '</br>';
echo form_submit('submit', 'Log in');
?>
<br/><br/>
Forgot Password <br/>
New User? Register
</div>
The two lines commented out is how it was earlier. Now I'm getting the following error: Call to a member function add_row() on a non-object. Why is table a non-object? I've tried loading the library in the controller but the error persists. Please help!
In codeigniter, the global codeigniter object is not available in your views, so you have to get a reference to it.
Try the following
$ci =& get_instance();
Put that at the top, and replace your calls to $this like so:
$ci->load->library('table');
$ci->table->add_row('Username', form_input('username'));
echo $ci->table->generate();

HTML Anchor to calendar?

Is there a way to make a generic link to create a calendar event as an HTML anchor's URI? For instance, you can make a link that creates an email with Link text, and you can make a link that initiates a phone call with Link text. Is there such a syntax for calendars that is NOT program specific? (I know there are things like outlook:// and iCal can use .ics files, but I want a program-agnostic one)
I'm looking for something like <a href="cal:Event%20title?start=timestamp&end=timestamp&description=Arbitrary%20description%20text>Link text</a>
You'll need to use a combination of the webcal link as well as a dynamically created ics file. Usage of webcal would be as follows:
Link
However, for best compatibility, I think your best bet would be to just link directly to the ics file:
Link
Then, in the createCal.php file (or whichever your programming language of choice is) you could do something similar to this article to create an ics file on the fly that the user could import into their calendar program of choice.
Here's another sample I found for the contents of the PHP file (note that this isn't tested, but more so a starting point):
<?php
//Set the content-type of the file
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=Calendar.ics");
echo "BEGIN:VCALENDAR\n";
echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n";
echo "VERSION:2.0\n";
echo "METHOD:PUBLISH\n";
echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n";
echo "BEGIN:VEVENT\n";
echo "CLASS:PUBLIC\n";
echo "CREATED:".date('Ymd\THis', time())."\n";
echo "DESCRIPTION:".$_GET['description'];
echo "DTEND:".date('Ymd\THis', $_GET['end'])."\n";
echo "DTSTAMP:".date('Ymd\THis', time())."\n";
echo "DTSTART:".date('Ymd\THis', $_GET['start'])."\n";
echo "LAST-MODIFIED:".date('Ymd\THis', time())."\n";
echo "LOCATION:\n";
echo "PRIORITY:5\n";
echo "SEQUENCE:0\n";
echo "SUMMARY;LANGUAGE=en-us:".$_GET['title']."\n";
echo "TRANSP:OPAQUE\n";
echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000\n";
echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n";
echo "X-MICROSOFT-CDO-IMPORTANCE:1\n";
echo "X-MICROSOFT-DISALLOW-COUNTER:FALSE\n";
echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUE\n";
echo "X-MS-OLK-AUTOFILLLOCATION:FALSE\n";
echo "X-MS-OLK-CONFTYPE:0\n";
//Here is to set the reminder for the event.
echo "BEGIN:VALARM\n";
echo "TRIGGER:-PT1440M\n";
echo "ACTION:DISPLAY\n";
echo "DESCRIPTION:Reminder\n";
echo "END:VALARM\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>

set html dropdown to selected value after refresh

how do i set a dropdown menu to a get variable after refresh. there is an html menu and after a refresh i would like the dropdown to be set to the select variable. for example if i select 3 from the menu then click submit the dropdown should display 3. i used to do this with textboxes where i would set the value to the get variable im just trying to do this same technique with html dropdown menus.
</select>
<?php
$c=$_GET['c'];
$p=$_GET['p'];
$id=$_GET['id'];
if ($c!=NULL){
$sq=mysql_query("SELECT * FROM ps WHERE b='$id' AND c='$c'");
while ($row=mysql_fetch_assoc($sq)) {
$start=$row['start'];
$start=trim($start);
$m=$row['m'];
}
echo "<select type='text' name='pro' id='amount' value= '$p'>";
echo "<option value=''>P</option>";
while ($start<=$m){
echo "<option value='$start'>$start</option>";
$start++;
}
}
?>
</select>
Is this in a form? If so you echo options like this:
echo "<option value='$start'";
if (isset($_POST['pro']) && ($_POST['pro']==$start)) echo " selected='selected'";
echo ">$start</option>\n";
But first you need to submit the form though.
The idea is that you compare the value of a set variable for this select element to current value of $start within the loop, and print 'selected' if they match.
Is this what you meant??
http://www.plus2net.com/php_tutorial/pb-drop.php