PHP/MySQL Wierdness

T

Testin da Cable

Guest
PHP/MySQL werdness

well, I've been looking at this test-script's code for ages so I expect it's right before my eyes...but I just don't see it.

the problem: the insert into the database doesn't happen. why? I dunno :( the select query works, the insert doesn't.

I want to put data from a variable with the name newnews into the database if the form's 'submit' button gets clicked, changing it's value to SUBMIT. it no workie. help0r please!

Code:
<?

include ("dbc.php");

// news has been submitted
if ($submit == "SUBMIT") {
dbConnect("www");
$sql = "INSERT INTO news SET
        data = '$newnews'";
        if (mysql_query($sql)) {
        echo("

The news has been added to the database!</p>");
        } else {
        echo ("

Error adding news!!</P>");
        }
mysql_close();
}
?>

<html>
here are the current newsitems:


<? // get current news

dbConnect("www");
$query = ("SELECT * FROM news");
$result = mysql_query($query);
if ($result) {
        while ($r = mysql_fetch_array($result)) {
        $user = $r["user"];
        $data = $r["data"];
        $date = $r["date"];
        echo "$user, $data, $date
";
        }
mysql_close();
}
?>

add an item to the database?





<form action="<?=$PHP_SELF?>" method="POST">
<textarea wrap name="newnews" rows=15 cols=70></textarea>
<hr noshade color=orange>
<input type="submit" value="SUBMIT">
</form>
</p>
</html>
 
T

Testin da Cable

Guest
*bump*

ahem, I've been a lazy boy and not worked on this one any further. will try tonight though :(
 
J

Jonty

Guest
Hey TdC

I believe the problem is you're not actually executing the insert query. You've specified what you want the query to do in the $sql variable, but then you need to include a line like the following:

Code:
<?

include ("dbc.php");

// news has been submitted
if ($submit == "SUBMIT") {
dbConnect("www");
$sql = "INSERT INTO news SET
        data = '$newnews'";
        [b]$insert = mysql_query($sql);[/b]
        if ([b]$insert[/b]) {
        echo("

The news has been added to the database!</p>");
        } else {
        echo ("

Error adding news!!</P>");
        }
mysql_close();
}
?>
That's my guess at what's going wrong, anyway :)

Kind Regards

Jonty

P.S. I don't think there should be an = in front of $PHP_SELF in the lower form's action="" attribute :)
 
T

Testin da Cable

Guest
ooh thanks Jonty! I've been looking at that code for so long that I couldn't even see it anymore heh. Now that you've pointed it out I realise -flash of insight- that I should be yelling "d'OH!" :D

as to the $PHP_SELF thing, I have no idea really. I found it on a website when I was looking for a way to find out how to shove input into the script. iirc they had the"=" in there. I'll give it a shot and find out :) thanks again!
 
S

(Shovel)

Guest
Originally posted by Testin da Cable ooh thanks Jonty! I've been looking at that code for so long that I couldn't even see it anymore heh.

Blond... brunet... redhead?

*Shovel assimilates for information on PHP for the uber content management system he has planned for Students Social...*
 
T

Testin da Cable

Guest
Ben, I don't know if you need it, but I can let you look at the scripts I'm making as I teach myself PHP. Now that Jonty's pointed out the (d'OH!) thing I was forgetting I can speed through my administration backend by reusing lots of the original script.

The thing I'm puzzling over now is the most efficient use of dates in the mysql tables that contain my site's content. I need to dream up some queries that get "latest article", "last 5 articles" etc. etc.
 
S

(Shovel)

Guest
Yeah - I mean, I'm going to spend a couple of weeks after my exams (in the middle of em now, I have Data Structures & Algorthms this afternoon, and, coincidently, Databases tomorrow).

We'vd been taught a decent handful of basic SQL on my course this year, and that's where I plan to build from. I also have FatbusinessMan working on it with me - who, as a 'genuine' Cambridge student, knows far more than he thinks he does.

PHP itself should be dead easy, it's just a new syntax. I've been playing with it a little in recent months, but then got back to doing work like a good boy... I did buy myself the Whitehead/Dasamero "Visual PHP" book though, which is a fantastic little syntax reference.
 
S

(Shovel)

Guest
In answer to your actual Question TD - when, after a day, it turns out far more difficult than I ever imagined, I'll come a running :D
 
T

Testin da Cable

Guest
naturally I'll be building all kinds of funkyness into the scripts, feeding my readers subliminal messages to aid my quest of world domination. sure, if I can help you then I will, but as you're getting this at skool while I'm teaching myself (while drinking beer and listening to Sigur Ros & Tool) I expect it is you who will be aiding me ;)
 
J

Jonty

Guest
Good luck with your exams Ben :D

As chance would have it, I'm going to be developing my own CMS soon as well (pft! another one :p). What I'd quite like to do, however, is utilise XML over MySQL where possible. Although this isn't practicle for forums, etc, it does have its uses. Also, if I manage to use the .NET framework, it has the added advantage that people can then write addons/modules in their prefered language and it will all, 'just work'. That said, PHP's .NET capabilities are ever chaning at the mo.

Perhaps we should start a CMS thread where everyone can contribute their ideas, bits of source code, questions and answers etc? Apart from us, I know a few other people who are going to start developing a CMS soon too.

Viva la revolution!
 
T

Testin da Cable

Guest
hmm, I'm still wrestling with inputting data into a mysql database. for the life of me I can't get INSERT queries generated by filling in a text input box to be processed correctly. I've made a small test code snippet to see if my code was actually doing anything, and lo and behold this also doesn't work. consider the folowing example:

Code:
<html>
<body>
<?php
  $variable = "It worked!!";
if ($submit == "Enter!") {
  echo "$variable";
} else {
?>

  <form method="post" action="<?$PHP_SELF?>">
  <input type="Text" name="data">

  <input type="Submit" name="submit" value="Enter!">
  </form>
<?php
}
?>
</body>
</html>

this creates a small text filed and a button. if the button gets pressed it should echo a salutation and the data entered in the field. it doesn't, so if anyone about can tell me why I'd be a very happy bunny.
 
J

Jonty

Guest
Hi TdC

I'd check your PHP setup, since I just uploaded that exact code to my BarrysWorld webspace and it worked perfectly :p Not what you wanted to here, I'm sure, but there you go :(

Kind Regards
 
T

Testin da Cable

Guest
thanks for trying it mate :) you're right though, that wasn't quite what I wanted to hear. the only thing I can come up with is that PHP doesn't agree with apache2. they say so themselves on their webpage so I guess it's up to me now.
 
J

Jonty

Guest
I think you might be on to something there, as I vaguely recall DBs saying as such when I was chatting to him once. I believe that's why BarrysWorld still runs Apache 1.3.27 (this is what you get for being all up to date ;) hehe).

Kind Regards
 
S

(Shovel)

Guest
For learning I'd get it running with Apache 1.3? I found that easier to set up, and it's presumably not going to be doing anything 2.0 specific?
 
W

wyrd_fish

Guest
Originally posted by Jonty
Perhaps we should start a CMS thread where everyone can contribute their ideas, bits of source code, questions and answers etc? Apart from us, I know a few other people who are going to start developing a CMS soon too.

I too am starting a CMS...
 
T

Testin da Cable

Guest
Originally posted by (Shovel)
For learning I'd get it running with Apache 1.3? I found that easier to set up, and it's presumably not going to be doing anything 2.0 specific?


well, I happen to like 2, isall :)


edit: taking out 2. going for an all new old version, php and ssl. riiight :)
 
T

Testin da Cable

Guest
well, I've moved from a 2 version of apache to a 1.3 version, and reinstalled php for good measure.


it still doesn't work :(

remember my test code?
Code:
<html>
<body>
<?php
  $variable = "It worked!!";
if ($submit == "Enter!") {
  echo "$variable";
} else {
?>

  <form method="post" action="<?$PHP_SELF?>">
  <input type="Text" name="data">

  <input type="Submit" name="submit" value="Enter!">
  </form>
<?php
}
?>
</body>
</html>

well, I added this to it:


Code:
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
    if (empty($varvalue)) {
        $empty[$varname] = $varvalue;
    } else {
        $post[$varname] = $varvalue;
    }
}

print "<pre>";
if (empty($empty)) {
    print "None of the POSTed values are empty, posted:\n";
    var_dump($post);
} else {
    print "We have " . count($empty) . " empty values\n";
    print "Posted:\n"; var_dump($post);
    print "Empty:\n";  var_dump($empty);
    exit;
}

it dumps everything that's in $_POST, and yes you guessed it...that actually works. use the script to submit something and I got this as output:

None of the POSTed values are empty, posted:
array(2) {
["data"]=>
string(17) "wthertjwrtjwertjh"
["submit"]=>
string(6) "Enter!"
}

so here we see that the array $_POST[submit] has a value, but there is nothing called $submit. wow, that opens possibilities I guess, so I modify the script:

Code:
<html>
<body>
<?php
if ($_POST[submit] == "Enter!") {
  echo "$_POST[data]";
?>
  <form method="post" action="<?$PHP_SELF?>">
  <input type="Text" name="data">

  <input type="Submit" name="submit" value="Enter!">
  </form>
<?php
}
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
    if (empty($varvalue)) {
        $empty[$varname] = $varvalue;
    } else {
        $post[$varname] = $varvalue;
    }
}
print "<pre>";
if (empty($empty)) {
    print "None of the POSTed values are empty, posted:\n";
    var_dump($post);
} else {
    print "We have " . count($empty) . " empty values\n";
    print "Posted:\n"; var_dump($post);
    print "Empty:\n";  var_dump($empty);
    exit;
}
?>
</body>
</html>

well will you look at that? it works now. funny that I still have the feeling it's going terribly wrong somewhere :/ I'm too much of a PHP n00b to pinpoint it, but I have the idea that I've got PHP itself b0rked somehow. hmmm.
 
W

wyrd_fish

Guest
wow... and so neat too...

whenever my code goes busted it gets tinkered with so much it starts to look like a bit of a mess...

like the time i tried to do page numbers, never got page numbers but got it to do next page and previous page links:

http://mooses.34sp.com/blue/art.php

Code:
	$art_num = mysql_num_rows($art_num_result);
	
	$art_show = mysql_num_rows($art_show_result);
	
	$next = $show_query_num + "9";
	$prev = $show_query_num - "9";
	
	$show_query_num_bork = $show_query_num + "9" ;
	
	if($art_num > "9" and $show_query_num > "1" and $show_query_num_bork < $art_num){
		print("

<font size=\"-1\"><a href=\"art.php?show_query_num=$prev\">Previous Page</a> - <a href=\"art.php?show_query_num=$next\">Next Page</a></font>");
	}elseif($art_num > "9" and $show_query_num > "1" and $show_query_num_bork > $art_num){
		print("

<font size=\"-1\"><a href=\"art.php?show_query_num=$prev\">Previous Page</a></font>");
	}elseif($art_num > "9" and $show_query_num < "1" and $show_query_num_bork < $art_num){
		print("

<font size=\"-1\"><a href=\"art.php?show_query_num=$next\">Next Page</a></font>");
	}elseif($art_num < "9"){
		print("

<font size=\"-1\">No other Pages.</font>");
	}else{
		print("

<font size=\"-1\">No other Pages.</font>");
	}
	
	if($art_num > "1"){
	    print("
<font size=\"-1\">There is a total of $art_num Articles.</font>");
    }elseif($art_num = "0"){
	    print("
<font size=\"-1\">There are currently no Articles.</font>");
    }else{
	    print("
<font size=\"-1\">There is a total of 1 Article.</font>");
    }
 

Users who are viewing this thread

Top Bottom