PDA

View Full Version : Help me solve this please


leonardi
12-18-2010, 05:35 PM
Hello,

First of all i am all new to PHP and stuff and i started following the videotutorials by David Jackson however his video's are kind of low quality so i can't read everything he writes in dreamweaver in his videos so here is my problem:
<?php
require_once("connection.php"); ///Refers to connection php file//
///////////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
///////////////////////////
if ($submit == $data2 == $data3 == $data4){

/////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('data2', 'data3', 'data4')");
mysql_query($query)or die(mysql_error(]);
/////////////////////////
}elseif[$submit]{
echo "one of your fields is empty!";
}

I know that the problem is in this line:
if ($submit == $data2 == $data3 == $data4)

The ='s aren't supposed to be ='s at all but i can't make out what they really should be from the movie so if anyone could help me out with this i'd be deeply appreciative

Sincerely,
Leonardi

edbr
12-18-2010, 11:54 PM
&& not == or you are asking for all to be equal

leonardi
12-19-2010, 11:15 AM
Thank you for replying, it worked =)

jmichae3
12-28-2010, 05:34 AM
yes, the proper expression is if ($submit == $data2 && $submit == $data3 && $submit == $data4) {

sounds like you've got it though.

edbr
12-28-2010, 05:40 AM
i think that was obvious by the answe dont you?
Thank you for replying, it worked =)

davidj
01-09-2011, 02:35 PM
I need to update those vids

they are a bit dated and need brushing up

jmichae3
01-10-2011, 06:57 AM
you had errors in your mysql_error() and if code where you used a square brackets instead of perens.

for the admin page you can use something that generates forms.

updates are generally accomplished using the UPDATE sql statement.
UPDATE table1 SET field2='blah',field3='blah',field4='blah' WHERE condition

if you don't set a condition, it will update all the rows in the table, sp be specific.

designing your database tables correctly and having them well-thought out is part of a good design.

writing down/diagramming your database design is also critical to your success 6 months later when you have to come back to the project and you wonder what you were thinkiong when you look at the code.
ER (Entity-Relationship) diagrams are made for this, or, if you are familiar with UML, you can use class diagrams for this purpose equally well. ArgoUML is a free diagram tool I use. http://argouml.tigris.org

just out of curiosity, how in your database design are you currently distinguishing between videos?
I mean in case you need to delete one, or select a group of them or something?
why are you not inserting ALL of your data? is there some reason for that?

If the table is for videos, 6 months later, you will kick yourself for not choosing better names for your tables and table columns. you will wonder "what on earth was I thinking? what is this?"
good names are critical to understanding.

proper code indentation is a good idea to help you understand your own code. do it always.




<form action="mypage.php" method="post" style="border-color:#0f0;padding:20px;float:right;border-width:thin;border-style:solid;">
<H2
<input type="text" name="data2" maxlength="255">
<input type="text" name="data3" maxlength="255">
<input type="text" name="data4" maxlength="255">
<input type="hidden" name="action" value="add">
<input type="submit">
</form><br style="clear:both;">
<?php
require_once("connection.php"); ///Refers to connection php file//
///////////////////////////
$data1 = $_POST['data'];
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$action = $_POST['action'];
///////////////////////////
if (isset($data1)
&& isset($data2)
&& isset($data3)
&& isset($data4)
&& isset($action)
&& 'add'==$action) {
//-----add
mysql_query("
INSERT INTO table1 (field2, field3, field4)
VALUES (
'" . mysql_real_escape_string($data2) . "',
'" . mysql_real_escape_string($data3) . "',
'" . mysql_real_escape_string($data4) . "'
)
") or die(mysql_error());
$last_insert_id=mysql_insert_id();
//-----just out of curiosity, how do you distinguish between videos?
//-----just out of curiosity, why don't you work with all your data?
} else if (isset($data1)
&& isset($data2)
&& isset($data3)
&& isset($data4)
&& isset($action)
&& 'change'==$action) {
//-----change
mysql_query("
UPDATE table1
SET
field2='" . mysql_real_escape_string($data2) . "',
field3='" . mysql_real_escape_string($data3) . "',
field4='" . mysql_real_escape_string($data4) . "'
WHERE
condition
") or die(mysql_error());
//-----just out of curiosity, how do you distinguish between videos?
//-----just out of curiosity, why don't you work with all your data?
} else if (isset($_POST['data1'])
&& isset($data2)
&& isset($data3)
&& isset($data4)
&& isset($action)
&& 'delete'==$action) {
//-----delete
mysql_query("
DELETE
FROM table1
WHERE
condition
") or die(mysql_error());
//-----just out of curiosity, how do you distinguish between videos?
//-----just out of curiosity, why don't you work with all your data?
} else if ($isset($action) {
echo "<div style='color:red;'>one of your fields is empty!</div>";
}
$result = mysql_query("SELECT * FROM into table1 WHERE condition ORDER BY table_column_name") or die(mysql_error());
$editorcount=1;
while ($row=mysql_fetch_assoc($result) {
?>
<form action="mypage.php" method="post" style="border-color:#00f;padding:20px;float:right;border-width:thin;border-style:solid;">
<h2>Editor #<?php echo $editorcount; ?></h2>
<input type="text" name="data2" maxlength="255" value="<?php echo $row['data2']; ?>">
<input type="text" name="data3" maxlength="255" value="<?php echo $row['data3']; ?>">
<input type="text" name="data4" maxlength="255" value="<?php echo $row['data4']; ?>">
<select name="action">
<option selected value="change">Change</option>
<option value="delete">Delete</option>
</select>
<input type="submit">
</form><br style="clear:both;">
<?php
}
mysql_free_result($result);
/////////////////////////


this is a long list of editors.

davidj
01-11-2011, 03:26 PM
I am an advocate of keeping logic separate from output. Especially in procedural code but I would champion a database class to implement the above.

one method could perform all your validation and another could handle your SQL

Its the way forward.

jmichae3
01-12-2011, 12:09 AM
I am an advocate of keeping logic separate from output. Especially in procedural code but I would champion a database class to implement the above.

one method could perform all your validation and another could handle your SQL

Its the way forward.
properly implemented, nothing should have access to data members - all access is through methods and constructors. so you could implement an extra method like maybe fetch_row() that either returns an associative fetched row array from the database, or returns FALSE, for your output.

I have not yet made the move to classes.