Autor | Zpráva | ||
---|---|---|---|
wizardik Profil * |
#1 · Zasláno: 18. 12. 2011, 13:18:02
Dobrý den mám problém s updatem článku..Nějak takto vypadá můj skript ale do okénka se nevypíšou věci které dělám pomocí value.Neví někdo čím to je? Můj soubor funkce.php vypadá takto(jen ta jedna funkce):
funkce.php function update_article_form($id){ $id = mysql_real_escape_string($id); $sql = "SELECT * FROM articles WHERE id='$id'"; $res = "mysql_query($sql) or die (mysql_error())"; $row = "mysql_fetch_assoc($res)"; ?> <form action="index.php" method="post" name="update"> <input type="hidden" name="update" value="true" /> <input type="hidden" name="id" value="<?-$row['id']?>" /> <p>TITLE:</p> <input type="text" name="title" value="<?-$row['title']?>" /> <p>BODY</p><textarea cols="100" rows="15" name="body"><?-$row['body']?></textarea> <input type="submit" name="submit" value="UPDATE" /> </form> <?php } <?php header("Content-Type: text/html; charset=windows-1250"); include '../funkce.php'; $obj = new MyBlog(); $obj->host = "localhost"; $obj->user = "root"; $obj->pass = "lokomotiva"; $obj->db = "MyOwnBlog"; //Connect to my DB $obj->connect(); mysql_query("SET NAMES 'cp1250'") or die('Could not set names'); ?> <html> <head><link rel="stylesheet" href="../style.css"> <title>My Own Blog(Sabbath)</title> </head> <body> <div id="page-wrap"> <h1>Update Article!</h1> <?-$obj->update_article_form($_GET['id']);?> </div> </body> </html> |
||
Tori Profil |
#2 · Zasláno: 18. 12. 2011, 15:29:42
Co je tohle za zkratku?
<?- Pro echo se tuším používá <?=
|
||
wizardik Profil * |
#3 · Zasláno: 18. 12. 2011, 15:35:12
Aha tak v tom byl problém děkuji vám mockrát..
|
||
wizardik Profil * |
#4 · Zasláno: 18. 12. 2011, 19:10:01 · Upravil/a: wizardik
Tak mám ještě jeden menší problém takhle mám index.php
<?php header("Content-Type: text/html; charset=windows-1250"); include '../funkce.php'; $obj = new MyBlog(); $obj->host = "localhost"; $obj->user = "root"; $obj->pass = "lokomotiva"; $obj->db = "MyOwnBlog"; //Connect to my DB $obj->connect(); mysql_query("SET NAMES 'cp1250'") or die('Could not set names'); ?> <html> <head><link rel="stylesheet" href="../style.css"> <title>My Own Blog(Sabbath)</title> </head> <body> <div id="page-wrap"> <?include 'nav.php'?> <?php if($_POST['update']): $obj->update_article($_POST); endif; ?> </div> </body> </html> function update_article($p){ $title = mysql_real_escape_string($p['title']); $body = mysql_real_escape_string($p['body']); $id = mysql_real_escape_string($p['id']); if(!$title || !$body): if(!$title): echo 'Title is bad!'; endif; if(!$body): echo 'Body is bad'; endif; echo '<p><a href="update-content.php?id=' . $id . '">Try Again</a></p>"'; else: $sql = "UPDATE articles SET title = '$title', body = '$body' WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); echo'Update Succesfully'; endif; } chyba je toto : Fatal error: Call to undefined method MyBlog::update_article() in C:\ComplexWebServer\http_docs\MyOwnBlog\admin\index.php on line 22 |
||
Bertram Profil |
#5 · Zasláno: 18. 12. 2011, 20:16:16
wizardik:
Ahoj, není to funkce, ale metoda třídy(objektu). V tomto případě se jedná o objekt $obj vytvořený z třídy MyBlog. Ten způsob, jakým ji na 22 řádku voláš značí, že bez magie by tato metoda musela být veřejná, jinak nelze volat z vnějšího kódu. To že musí v třídě existovat je samozřejmostí. |
||
wizardik Profil * |
#6 · Zasláno: 18. 12. 2011, 20:23:39
Dobrá ale jak to mám opravit?
|
||
Bertram Profil |
#7 · Zasláno: 18. 12. 2011, 20:32:07
wizardik:
Musel by jsi se podělit o kód třídy MyBlog. |
||
wizardik Profil * |
#8 · Zasláno: 18. 12. 2011, 22:09:51
Žádný problém :D
<?php class MyBlog{ var $host; var $username; var $password; var $db; function connect(){ $conn = mysql_connect($this->host,$this->username,$this->password)or die(mysql_error()); mysql_select_db($this->db,$conn) or die (mysql_error()); } function add_article($id = ''){ if($id != ''): $sql = ("SELECT * FROM articles WHERE id = '$id'"); $id = mysql_real_escape_string($sql); else: $sql = "SELECT * FROM articles ORDER BY id DESC"; endif; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res)!=0): while($rom = mysql_fetch_assoc($res)) { echo '<h1><a href="index.php?id=' . $rom['id'] ,'">' , $rom['title'],'</a></h1>'; echo '<p>' , $rom['body'] , '</p>'; } else: print ('Page not found!'); endif; } function ViewArticle($id = ""){ if($id != ''): $sql1 = ("SELECT * FROM articles WHERE id = '$id'"); $id = intval($sql1); else: $sql1 = "SELECT * FROM articles ORDER BY id DESC"; endif; $res1 = "mysql_query($sql1)" or die(mysql_error()); $num_rows = (mysql_num_rows($res1)); if(!$num_rows): echo 'Page not found'; else: echo '<h1><a href="/admin/edit-article.php?id=' . $rom1['id'] , '">',$rom1['title'],'</a></h1>'; echo '<textarea cols=' .'"100"' . 'rows='.'"15"' . 'name=' . '"body"'.'>'. $rom1['body'] .'</textarea>'; endif; } function manage_article(){ echo '<div id="manage">'; $sql = ("SELECT * FROM articles"); $res = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_assoc($res)): ?> <h2 class="title"><?=$row['title']?></a></h2> <span class="actions"><a href="update-article.php?id=<?=$row['id']?>">Edit</a>|<a href="?delete=<?=$row['id'];?>">Delete</a></span> <?php endwhile; } function delete_article($id) { if(!$id){ return false; }else{ $id = mysql_real_escape_string($id); $sql = "DELETE FROM articles WHERE id=$id"; $res = mysql_query($sql) or die(mysql_error()); echo 'Delete article was Sucessfuly' . '<a href="' . 'index.php"' . '>' . 'You can go Back' . '</a>'; } } function update_article_form($id){ $id = mysql_real_escape_string($id); $sql = "SELECT * FROM articles WHERE id='$id'"; $res = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($res); ?> <form action="index.php" method="post" name="update"> <input type="hidden" name="update" value="true" /> <input type="hidden" name="id" value="<?=$row['id']?>" /> <p>TITLE:</p> <input type="text" name="title" value="<?=$row['title']?>" /> <p>BODY:</p><textarea cols="100" rows="15" name="body"><?=$row['body']?></textarea> <input type="submit" name="submit" value="UPDATE" /> </form> <?php function update_article($p){ $title = mysql_real_escape_string($p['title']); $body = mysql_real_escape_string($p['body']); $id = mysql_real_escape_string($p['id']); if(!$title || !$body): if(!$title): echo 'Title is bad!'; endif; if(!$body): echo 'Body is bad'; endif; echo '<p><a href="update-content.php?id=' . $id . '">Try Again</a></p>"'; else: $sql = "UPDATE articles SET title = '$title', body = '$body' WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); echo'Update Succesfully'; endif; } } }//End class ?> |
||
Davex Profil |
#9 · Zasláno: 18. 12. 2011, 22:21:27
Metoda
update_article() je uvnitř jiné, takže není vidět navenek. Musíš jí dát do třídy samostatně.
|
||
wizardik Profil * |
#10 · Zasláno: 19. 12. 2011, 00:40:23
Funguje Děkuji!
|
||
Časová prodleva: 12 let
|
0